Interact with Dynamic Object Created



  • Hi everyone, this is my 2nd post on qt-forum and i'm stuck in a stupid issue.
    I've created a custom object called "TabTPDef.qml", and I use this object min. 4 times.
    I've created its with the following code, when the user click on the button.

     var component;
                           var sprite;
                           component = Qt.createComponent("TabTPDef.qml");
                           sprite = component.createObject(dhsbLink, {"x": 0, "y": 0});
    

    Now, in TabTPDef there is a button which call a C++ method to populate the 'TabTPDEF'. The populating works well, but it works for the all the TabTPDef instantiated. The onClicked for call the c++ method is descripted in 'TabTPDef' and i suppose the problem is here, because there isn't reference to the new component instantiated.

    Here how I create the new component:

    buttTp2.mouseButton.onClicked: {
        var component;
                           var sprite;
                           component = Qt.createComponent("TabTPDef.qml");
                           sprite = component.createObject(dhsbLink, {"x": 0, "y": 0,});
     
        sprite.anchors.top = parent.top
        sprite.anchors.topMargin = 100
        sprite.anchors.left = parent.left
        sprite.anchors.leftMargin = 450
        sprite.anchors.bottom = dhsbLink.bottom
        sprite.frateto.txtButt.text = "TP2";
        sprite.frateto.buttonMain.state = "off"
    
    }
    

    This is the TabTPDef, which contains other two custom object

    Item {
        id: tabTPDEF
        height: 700
        width: 325
        property alias soreta : soreta
        property alias frateto : frateto
    
        SwipeView {
            id: swipe
           currentIndex: 0
           clip: true
            anchors.fill: parent
    
            TabTPMonitor{
            id: frateto
            }
            TabTP{
             id: soreta
            }
        }
    ....
    }
    

    Sorry for the mistakes, i'm still learning qt, and my english is not so well.
    Thank you!



  • @AboutClod

    http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html
    As a guess you're object isn't there when you tried using it? It may also have failed (maybe your component source is invalid / producing errors).

    You've missed all of this part of creating objects dynamically:

    if (component.status == Component.Ready)
            finishCreation();
        else
            component.statusChanged.connect(finishCreation);
    }
    
    function finishCreation() {
        if (component.status == Component.Ready) {
            sprite = component.createObject(appWindow, {"x": 100, "y": 100});
            if (sprite == null) {
                // Error Handling
                console.log("Error creating object");
            }
        } else if (component.status == Component.Error) {
            // Error Handling
            console.log("Error loading component:", component.errorString());
        }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.