动态存储QML对象

时间:2016-02-13 12:40:36

标签: qt qml qt5 qvariant

我希望存储一个变量或列表,一组动态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)
}

我该怎么做?

编辑:我附上了我的两个文件: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()

    }


}

1 个答案:

答案 0 :(得分:3)

这样的事情应该有效:

property var objs : []

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

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

for (var i=0; i<10; i++){
    console.log(objs[i])
}

在添加代码后进行修改:

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++
   }

}