Search code examples
qtqmlqt5qvariant

Storing dynamically QML objects


i want to store in a variant or a list, a set of dynamically QML created objects.

when i do it once, it works nice:

property var obj

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

i am trying to iterate 10 times to create a 10 length object

property variant objs

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

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

How can i do it?

Edit: i attach you my 2 files: main.qml and 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()

    }


}

Solution

  • Something like this should work:

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

    Edit after your added code:

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