我有一个类似的结构:
import QtQuick 2.0
Item {
Item {
Component.onCompleted: console.log("foo ", parent.gridMap)
}
Component.onCompleted: console.log("bar ", gridMap)
}
此项目将通过createObject(baz, {"gridMap": gridMap})
从另一个项目创建,其中gridMap: []
。
为什么我会得到类似以下内容的输出?
bar []
foo undefined
就我而言,如果我尝试您的代码,我会得到错误"gridMap未定义"。
因此,只要在组件中声明"gridMap"就可以了,QML无法添加新属性,只需将初始值传递给存在的属性。。。
import QtQuick 2.0;
Rectangle {
width: 360;
height: 360;
Component.onCompleted: { // try your code
var obj = component.createObject (baz, { "gridMap" : [ 3, 5, 9] });
}
Row {
id: baz;
// for newly created items
}
Component { // the component to instanciate
id: component;
Item {
property var gridMap;
Item {
Component.onCompleted: console.log("foo ", parent.gridMap)
}
Component.onCompleted: console.log("bar ", gridMap)
}
}
}