2012-11-27 42 views
4

我无法尝试使用Javascript在QML中生成对象以动态创建对象。如何用变量动态创建对象?

我想使用的代码是这样的

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() 
} 

,工作正常,但我想包括变量,使他们每个人不同的,所以,当我将信息传递给Button.qml它集以下

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

我想不出来,任何帮助将是伟大的,谢谢你们。

回答

3

Here的方法文件Component.createObject

正如你所看到的,你可以使用函数的第二个可选参数来设置新对象的参数。在你的情况下,它会是:

component.createObject(numbers, {"text": "1", "id": "button1"}); 
+0

是的字面上,你输入,我只是想出来,非常感谢:) – Steve