动态存储 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

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

}

这样的事情应该可以工作:

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

相关内容

  • 没有找到相关文章

最新更新