How to load a Element dynamically into already dynamically loaded qml file.



  • Hi All,

    I am loaded a qml file dynamically in the following manner.

    @function renew()
    {
    comp = Qt.createComponent("PushButtonAct.qml");
    // if(model.controltype.PushButton)
    console.log("model.controltype.PushButton",model.controltype);
    if(model.controltype==="PushButton"){
    if (comp.status === Component.Ready) {
    pushButton = comp.createObject(root, {"helpText" : model.pushButtonViewModel.helpText});

            if (pushButton === null) {
                console.log("Error creating object PushButtonAct");
            }
        } else if (comp.status === Component.Error) {
            console.log("Error loading component:", comp.errorString());
        }
    }
    

    }
    @

    depending on the condition if(model.controltype==="PushButton")
    i want to show a Element in the PushButtonAct.qml.

    how to do this.?

    Thanks in advance.


  • Moderators

    Hi,

    @
    pushButton = comp.createObject(root, {"helpText" : model.pushButtonViewModel.helpText});
    @

    Is pushButton null after this ?



  • No it will be there .



  • I want to load another Rectangle like element in the PushButtonAct.qml dynamically.


  • Moderators

    The approach is correct. Any other errors ?



  • Hi ,

    Actually I have a ToggleButton component in my folder and i want to call that one in PushButtonAct.qml and code is as folows

    @
    function renew()
    {
    comp = Qt.createComponent("PushButtonAct.qml");
    // if(model.controltype.PushButton)
    console.log("model.controltype.PushButton",model.controltype);
    if(model.controltype==="PushButton"){
    if (comp.status === Component.Ready) {
    pushButton = comp.createObject(root, {"helpText" : model.pushButtonViewModel.helpText});
    pushButton = comp.createObject(root, {"rectanglepopulate": ToggleButton});

            if (pushButton === null) {
                console.log("Error creating object PushButtonAct");
            }
        } else if (comp.status === Component.Error) {
            console.log("Error loading component:", comp.errorString());
        }
    }
    

    }
    @

    I am getting ReferenceError: Can't find variable: ToggleButton.

    In PushButtonAct.qml i have

    @
    Rectangle{
    id: root;
    property alias helpText : text1.text;
    property alias rectanglepopulate: rectanglepopula

    Rectangle {
    id:rectanglepopula
    x:40
    y:70
    width:75
    height:30
    }
    }
    @

    How to find the Reference of ToggleButton in .js file. ?


  • Moderators

    AFAIK, you will need to instantiate ToggleButton before using it in the JS. For eg:
    @
    property QtObject toggleButton: Qt.createComponent("ToggleButton.qml")
    @

    Then in JS you can get reference to toggleButton.


Log in to reply
 

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