2016-02-13 99 views
0

我想存儲一個變體或列表,一組動態QML創建的對象。存儲動態QML對象

當我做一次,它的工作原理很好:

property var obj 

var component = Qt.createComponent("MyObject.qml")    
obj = componente.createObject(contenedor) 

我試圖重複10次創造10長度對象

property variant objs 

var component = Qt.createComponent("MyObject.qml") 

for (var i=0; i<10; i++){ 
    objs[i] = component.createObject(contenedor) 
} 

我該怎麼辦呢?

編輯:我附上你我的2個文件:main.qml和MyObject.qml

main.qml

import QtQuick 2.1 
import QtQuick.Window 2.1 

Rectangle { 
    visible: true 
    width: 1920 
    height: 1080 
    color: "white" 
    id: contenedor 

    property variant colors: ['blue', 'red', 'gray', 'orange'] 
    property var arrayObjects: [] 
    property int currentObj: 0 

    property var singleObject 

    Component.onCompleted: init() 

    function init(){ 

     var componente = Qt.createComponent("MyObject.qml")    
     //singleObject = componente.createObject(contenedor,{"x":50,"y":10}) 
     //singleObject = componente.createObject(contenedor) 

     for (var i=0; i<colors.length; i++){ 
      arrayObjects.push(componente.createObject(contenedor)) 
     } 

     for (var i=0; i<colors.length; i++){ 
      console.log(arrayObjects[i]) 
     } 

     next() 
    } 

    function next(){ 
     console.log("Next"); 

     if(currentObj > colors.length){ 
      currentObj-- 
     }else if(currentObj===colors.length){ 
      console.log('--------------------Reset') 
      currentObj = 0 
     } 

     console.log("Index: " + currentObj) 

     arrayObjects[currentObj].visible = true 
     arrayObjects[currentObj].color = colors[currentObj] 
     arrayObjects[currentObj].end.connect(next) 
     arrayObjects[currentObj].init() 

     /*singleObject.visible = true 
     singleObject.color = colors[currentObj] 
     singleObject.end.connect(next) 
     singleObject.init()*/ 


     currentObj++ 
    } 

} 

MyObject.qml

import QtQuick 2.1 

Rectangle { 
    visible: true 
    anchors.fill: parent 
    id: object 

    signal end() 

    Timer { 
     id: timer 
     running: false 
     repeat: false 
     onTriggered: end() 
    } 

    function init(){ 
     console.log('Init object') 
     timer.interval = 5000 
     timer.start() 

    } 


} 

回答

3

喜歡的東西這應該工作:

您添加的代碼後

編輯:

import QtQuick 2.4 
import QtQuick.Window 2.2 
import QtQml.Models 2.2 

Window { 
    visible: true 
    width: 920 
    height: 500 
    color: "white" 
    id: contenedor 

    property variant colors: ['blue', 'red', 'gray', 'orange'] 
    property var arrayObjects: [] 
    property int currentObj: 0 
    property int zIndex: 1 

    property var singleObject 

    Component.onCompleted: init() 

    function init(){ 

     var componente = Qt.createComponent("MyObject.qml") 
     //singleObject = componente.createObject(contenedor,{"x":50,"y":10}) 
     //singleObject = componente.createObject(contenedor) 

     for (var i=0; i<colors.length; i++){ 
      arrayObjects.push(componente.createObject(contenedor)) 
     } 

     for (var i=0; i<colors.length; i++){ 
      console.log(arrayObjects[i]) 
     } 

     next() 
    } 

    function next(){ 
     console.log("Next"); 

     if(currentObj > colors.length){ 
      currentObj-- 
     }else if(currentObj===colors.length){ 
      console.log('--------------------Reset') 
      currentObj = 0 
     } 

     console.log("Index: " + currentObj) 

     arrayObjects[currentObj].visible = true 
     console.log("Color: " + colors[currentObj]); 
     arrayObjects[currentObj].color = colors[currentObj]; 
     arrayObjects[currentObj].z = zIndex; 
     arrayObjects[currentObj].end.connect(next) 
     zIndex++; 
     arrayObjects[currentObj].init() 

     /*singleObject.visible = true 
     singleObject.color = colors[currentObj] 
     singleObject.end.connect(next) 
     singleObject.init()*/ 


     currentObj++ 
    } 

} 
+0

那麼它似乎加載(因爲執行console.log),但在我的例子,我得到了一個空白屏幕而不是彩色幻燈片。我已經在上面添加了我的示例。謝謝。 – walolinux

+0

查看我編輯的代碼。你的問題是'z'索引。你必須調整堆疊。否則,新的組件將被隱藏起來。 – sk2212