2013-06-28 70 views
1

访问动态添加的属性我有一个类似的结构:通过createObject(baz, {"gridMap": gridMap})其中gridMap: []QtQuick:不能从孩子

import QtQuick 2.0 

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

此项目将从另一个项目创建。

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

bar [] 
foo undefined 

回答

2

对我来说,如果我尝试你的代码,我会得到错误'gridMap is not defined'。

所以只是声明“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) 
     } 
    } 
}