Dynamic Qt.createQmlObject with ID



  • Hi,
    As you can see below I'm trying to communicate between two : Qt.createQmlObject

    In the 1st I create a "Rectangle" and in the second I create the "Text".
    I want the second, that anchored with the rectangl : Anchors.centerIn

    But it seems he does not see the ID of "Rectangle". Error : Error: Qt.createQmlObject(): failed to create object:
    If I erase << anchors.centerIn: "menuId_'+menu[i].id+' " >>, all work fine but the text is on same place.

    Where did I make a mistake?

     Component.onCompleted:
             {
                    var x=0;
                    var y=0;
                    var titi_debug=0;
                    var menu = JSON.parse(getData('object'));
                    for(var i = 0; i < menu.length; i++)
                    {
                        console.log("JSON Parse: " + menu[i].id);
                        console.log("JSON Parse: " + menu[i].name);
    
                        Qt.createQmlObject('import QtQuick 2.0;
                                                            Rectangle {id : "menuId_'+menu[i].id+'"; color: "red"; width: 50; height: 50; x: '+x+'; y:'+y+'}', tuile, "toto");
                        Qt.createQmlObject('import QtQuick 2.0;
                                                            Text {id : "textId_'+menu[i].id+'"; text: "'+menu[i].name+'"; color: "white"; anchors.centerIn: "menuId_'+menu[i].id+'"}', tuile, "tata");
                        y=y+100;
                        titi_debug=x+100;
                    }
    
    
                }

  • Moderators

    @Hergo id doesnot work in this case. See more info here.

    Also, note that while dynamically created objects may be used the same as other objects, they do not have an id in QML.


Log in to reply
 

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