我的结构类似于:
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
答案 0 :(得分:2)
就我而言,如果我尝试你的代码,我会收到错误'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)
}
}
}