2012-11-27 41 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