Variants in external QML file not initialized when on changed handlers called
-
This seems inconsistent behaviour, can someone please tell me why this breaks and how to get around it?
Starting with a default qtquick 2 application I add the following to the top level Rectangle in main.qml:
@ Item {
id: inititem
property real f0: 20
}Rectangle { id: one width: parent.width height: 20 color: "red" property var listt: [1, 2, 3] property real f0: inititem.f0 onF0Changed: { console.log("one f0 changed") console.log(listt) console.log(width) } } Two { id: two f0: inititem.f0 }@
and in the file Two.qml have:
@import QtQuick 2.0
Rectangle {
width: parent.width
height: 20
color: "blue"property var listt: [1, 2, 3] property real f0: 0 onF0Changed: { console.log("two f0 changed") console.log(listt) console.log(width) }
}@
To me the items with id "one" and "two" should be identical, except two is moved to an external file to help organize the code. However, after running the program I get the output:
bq. two f0 changed
undefined
0
one f0 changed
[1,2,3]
360Why is the variant list in the external QML file (and it's width) undefined when f0 is changed, while everything works when I keep it in a single QML file? How can I get around this?
Thanks
-
Hi,
You're printing that out in a signal handler for a property change signal. That means that if two.f0 is initialized before two.listt and two.width then you are seeing expected behaviour.
Remember, QML is declarative, so the order of property initialization is not defined by the order it appears in the code (that would be imperative, not declarative).
You should wait until the Component.onCompleted signal is emitted - at that point, all of the property initializations are guaranteed to be complete.
Cheers,
Chris.