QML属性绑定取决于同一组件中的其他绑定

时间:2016-07-28 13:11:20

标签: qt qml

假设我有一个QtObject,它有一些属性。在同一QtObject中的其他绑定中使用绑定是否有任何缺点:

QtObject {
    // This is set by the caller
    property var myObject

    readonly property bool haveMyObject: myObject ? true :  false
    readonly property bool isSomething1: haveMyObject ? myObject.isSomething1 :  false
    readonly property bool isSomething2: haveMyObject ? myObject.isSomething2 :  false
    readonly property bool isSomething3: haveMyObject ? myObject.isSomething3 :  false
    readonly property bool isSomething4: haveMyObject ? myObject.isSomething4 :  false
    readonly property bool isSomething5: haveMyObject ? myObject.isSomething5 :  false
}

而不是:

QtObject {
    // This is set by the caller
    property var myObject

    readonly property bool haveMyObject: myObject ? true :  false
    readonly property bool isSomething1: myObject ? myObject.isSomething1 :  false
    readonly property bool isSomething2: myObject ? myObject.isSomething2 :  false
    readonly property bool isSomething3: myObject ? myObject.isSomething3 :  false
    readonly property bool isSomething4: myObject ? myObject.isSomething4 :  false
    readonly property bool isSomething5: myObject ? myObject.isSomething5 :  false
}

哪一个更好?我之所以要问的原因是因为Qt文档的评估顺序是未定义的。

1 个答案:

答案 0 :(得分:2)

订单完全未定义并非如此。绑定按其依赖关系定义的顺序进行评估。对于任何dependant-dependency对,保证在dependency之前评估dependant方。因此,在之后评估/更新每个isSomething属性它已经评估/更新了依赖项haveMyObject