Alias.qml
import QtQuick 2.0
Rectangle {
width: 100
height: 62
property var arr : [1,2,3,4]
}
Access.qml
import QtQuick 2.0
Rectangle {
id: newq
width: 100
height: 62
property var yy : [1]
property var pp : [1]
onPpChanged:
{
console.log("\npp: " + pp.pop())
console.log("\nyy: " + newq.yy.length + "\n")
}
}
main.qml
import QtQuick 2.0
Rectangle {
id: root
width: 360
height: 360
Alias
{
id: poi
}
Access
{
pp: poi.arr
}
}
The error shows up on this line:
console.log("\nyy: " + newq.yy.length + "\n")
Where am I going wrong?
This happens because the yy
property has not yet been initialised when pp
is changed. You can see this by changing Access.qml
:
import QtQuick 2.0
Rectangle {
id: newq
width: 100
height: 62
property var yy : [1]
property var pp : [1]
onYyChanged: console.log("yy changed: " + yy)
onPpChanged:
{
console.log("pp changed: " + pp)
console.log("\nyy: " + newq.yy.length + "\n")
}
}
This outputs:
qml: pp changed: 1,2,3,4
file:///E:/Dev/Projects/qt/qml-test/Access.qml:17: TypeError: Cannot read property 'length' of undefined
qml: yy changed: 1
You can see that yy
is eventually initialised, but not before pp
. You should guard against this with an if (newq.yy)
check, or refactor the code to avoid this situation, if possible.
The order of property assignments and bindings should not be relied upon, as QML is a declarative language.
Some related reading:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments