如何在分配之前验证Qml属性值?

时间:2015-07-10 12:14:52

标签: qt qml qtquick2 qtdeclarative

Element.qml

Item{
    property int step
    property int minValue
    property int maxValue
    property int value  //should accept values of form i*step+minValue
}

main.qml

Element{
    step:2
    minValue:-7
    maxValue:7
    value:0  //not an acceptable  value(2*i-7), should convert to 1 or -1
}

如果我设置了值,它应该将值转换为最接近的step*i+minValue, 即01,然后发出valueChanged()

我希望valueChanged()信号仅在其值step*i+minValue时发出。

2 个答案:

答案 0 :(得分:1)

您无法进行预测试,并且会在其上发送valueChanged信号。 唯一的解决方案是在onValueChanged插槽中进行测试并发送自定义信号,你最不发送valueChanged()信号,因为它连接到onValueChanged Slot。

<?php

// this file connects to the database
include("includes/connect.inc.php");

session_start();

if (isset($_SESSION['id'])) {
    $uid        = $_SESSION['id'];
} 

// //If the search input was submitted then run
if(isset($_POST['search'])){
    // turn that the user searched into a varible
    $searchQ = $_POST['search'];
    // delete any symbols for security
    $searchQ = preg_replace("#[^0-9a-z]#i", "", $searchQ);
    // Define varibles before the loop
    $searchArray = array();
    $searchIndex = 0;

    // Search through these columns inside the main database
    $searchQuery = mysqli_query("SELECT * FROM database WHERE 
        title   LIKE '%" . mysqli_escape_string( $searchQ ) . "%' or 
        date    LIKE '%" . mysqli_escape_string( $searchQ ) . "%' 
    ");

    // count the number of results
    $searchCount = mysqli_num_rows($searchQuery);
    if($searchCount != 0){
        while($row = mysqli_fetch_array($searchQuery)){
            $titleSearch     = $row['title'];
            $dateSearch      = $row['date'];

            // buile the array which will hold all the results
            $searchArray[$searchIndex] = array($titleSearch, $dateSearch);
            $searchIndex++;
        }
    }
}
// End of search php

答案 1 :(得分:0)

据我所知,无法设置属性验证器。可能的解决方法如下所示。例如,考虑只接受正数的属性。这样的属性可以编码如下:

Item {
    property int value: 0
    property int _value: 0
    on_ValueChanged: {
        if(_value > 0)
            value = _value;
    }
}

如果Item是您的自定义类,则可以在C ++中使用相同的方法。