admin管理员组文章数量:1415664
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?
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?
Share Improve this question asked Jun 11, 2014 at 10:01 Aquarius_GirlAquarius_Girl 23k71 gold badges249 silver badges441 bronze badges 2- Check if this might be helpful: qt-project/forums/viewthread/15244 and qt-project/doc/qt-5/qml-var.html – trainoasis Commented Jun 11, 2014 at 10:07
- Perhaps you should use "variant" instead of var there? (see second code block here: qt-project/doc/qt-4.8/qml-variant.html) – trainoasis Commented Jun 11, 2014 at 10:14
1 Answer
Reset to default 4This 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:
- QML Applications
- JavaScript Expressions in QML Documents
- Property Binding
本文标签: javascriptTypeError Cannot read property 39length39 of undefined in QML arrayStack Overflow
版权声明:本文标题:javascript - TypeError: Cannot read property 'length' of undefined in QML array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745223575a2648504.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论