QML Binding is inversed
-
I have the following QML followed from the project "here":http://qt-project.org/forums/viewthread/42408/
@ MeraCar {
id: car
username: "webiopi"
password: "raspberry"
ipAddress: "192.168.1.3"
portNo: 8000
forward_GPIO: 2
backward_GPIO: 3
right_GPIO: 4
left_GPIO: 17
}@Problem is the binding occurs from bottom to top. When the program starts, i need to first set the username, IP, password so as to be able to send packets to configure the respective GPIO pins on my raspberry pi. I have modified the mutator methods of the attributes to take care of this automatically.
But the GPIO are set first causing the configuration packet to go to a blank URL. I solved it by reshuffling the QML as this:
@ MeraCar {
id: car
forward_GPIO: 2
backward_GPIO: 3
right_GPIO: 4
left_GPIO: 17
username: "webiopi"
password: "raspberry"
ipAddress: "192.168.1.3"
portNo: 8000
}@As a simple developper, i'd expect the binding to start from top to bottom.
-
QML does not define the order, and you should not rely on it: the engine is free to choose which property should be updated and which should be skipped.
If you want to be certain of the order and timing, and you do not need the actual binding updates, you can use the onCompleted slot:
@
MeraCar {
id: carComponent.onCompleted: {
username = "webiopi"
password = "raspberry"
ipAddress = "192.168.1.3"
portNo = 8000
forward_GPIO = 2
backward_GPIO = 3
right_GPIO = 4
left_GPIO = 17
}
@ -
Property declarations in QML are declarative, not imperative. There is no (explicit or implied) ordering of when declarations will be resolved by the engine. If you do need such guarantees, then you should instead call an imperative init() function which sets those properties in the order you expect.
Note that the engine implementation may change (to achieve better optimization, for example) in a subsequent patch release which might change the order in which those declarations are resolved, which would "break" your code, so your workaround is a dangerous one.
Cheers,
Chris.