4

I'm having trouble trying to generate objects in QML using Javascript to create them dynamically.

The code I am trying to use is this

Grid {
    id: numbers
    anchors.centerIn: parent
    columns: 3
    spacing: 2
    function createNumbers(){
        var component = Qt.createComponent("Button.qml");
        for(var i=1; i<37; i++){
            component.createObject(numbers)
        }
    }
    Component.onCompleted: createNumbers()
}

Which works fine, however I want to include variables to make them each different, so that when I pass the information to Button.qml it sets the following

property string text: "1"
property string id: "button1"

I can't figure it out, any help would be great, thanks guys.

4

1 回答 1

3

是方法的文档Component.createObject

如您所见,您可以使用函数的第二个可选参数设置新对象的参数。在您的情况下,它将是:

component.createObject(numbers, {"text": "1", "id": "button1"});
于 2012-11-27T16:53:10.540 回答