【问题标题】:QtQuick: Cannot access dynamically added property from childrenQtQuick:无法从子级访问动态添加的属性
【发布时间】:2013-06-28 12:13:02
【问题描述】:

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

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

【问题讨论】:

    标签: dynamic qml qt-quick qtquick2


    【解决方案1】:

    就我而言,如果我尝试您的代码,我会收到错误“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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 2016-04-10
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      相关资源
      最近更新 更多