0
votes

I cannot find information about the exact execution order of QML property bindings and property change signals.

For instance I have a property like:

property int myProp: 1000

Then I have another property bound to it:

property int myProp2: myProp

And I have property change signal handlers:

onMyPropChanged: console.log("myProp changed: ", myProp, " myProp2: ", myProp2)
onMyProp2Changed: console.log("myProp2 changed: ", myProp2, " myProp: ", myProp)

If I set myProp to 2 for e.g. the output is:

qml: myProp changed:  2  myProp2:  1000
qml: myProp2 changed:  2  myProp:  2

I don't know if it is a rule that the property change signal runs first then the dependent properties are going to be updated next, or their execution order is not defined at all. What happens if I have more dependent properties, is their re-evaluation order defined?

Then finally what about cross dependencies like:

property int myProp3: myProp2 + myProp
property int myProp2: myProp
property int myProp: 1000

onMyPropChanged: console.log("myProp changed: ", myProp, " myProp2: ", myProp2, " myProp3: ", myProp3)
onMyProp2Changed: console.log("myProp2 changed: ", myProp2, " myProp: ", myProp, " myProp3: ", myProp3)
onMyProp3Changed: console.log("myProp3 changed: ", myProp3, " myProp: ", myProp, " myProp2: ", myProp2)

On my machine the output is:

qml: myProp changed:  2  myProp2:  1000  myProp3:  2000
qml: myProp2 changed:  2  myProp:  2  myProp3:  2000
qml: myProp3 changed:  4  myProp:  2  myProp2:  2
1

1 Answers

3
votes

The docs say:

If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.

But in QML, you don't really define things sequentially, so the order is really undefined.

In your first example, myProp2 is bound to myProp which means it's connected to myProp's onChanged signal. If any other properties are also bound to myProp, they will also be connected. But in what order? We can't tell.

So the moral of the story is you should not depend on the order of signals/slots in QML. Try to write your code in a way that the order does not matter.