1

我有一个类似于这样的结构:

import QtQuick 2.0

Item {
    Item {
        Component.onCompleted: console.log("foo ", parent.gridMap)
    }
    Component.onCompleted: console.log("bar ", gridMap)
}

该项目将通过createObject(baz, {"gridMap": gridMap})where从另一个项目创建gridMap: []

为什么我会得到类似于以下的输出?

bar []
foo undefined
4

1 回答 1

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)
        }
    }
}
于 2013-06-29T09:08:41.240 回答