2013-06-28 68 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) 
     } 
    } 
}