Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to acess a subobject of dynamically created object



  • Hi,
    I have a qml file called gasgroup.qml as below:
    Rectangle{
    id:gasgroup
    Row{
    id:row
    }
    Column{
    id:col
    }
    }

    I then create a object from this qml dynamically
    var gasobject = GasComponent.createobject(gasgrouop.qml);

    i want to access the rowobject(row) inside the gasgroup.qml from this dynamic object
    gasobject.Can someone explain how can i do so ?


  • Moderators

    Please use code tags around your code, it makes it much easier to read.

    i want to access the rowobject(row) inside the gasgroup.qml from this dynamic object gasobject.Can someone explain how can i do so ?

    There are a few ways to do it:

    • add an alias property in gasgroup pointing to your subobject. Then you can easily access it using gasobject.yourAliasProp
    • name the subobject (objectName property), then use QObject's findChild() method
    • use children property: gasobject.children[0]

    The alias property is by far the most elegant option.



  • @sierdzio said in How to acess a subobject of dynamically created object:

    meth
    Thanks for replying!
    I tries the property alias way by adding the below line to my "gasgroup.qml"
    "property alias row: row".
    I have a seperate JS file from which I am first creating a "gasgroupcomponent"
    and then using createobject() to create the object.

    Even after adding property alias in qml file still I am getting the below error:
    qrc:/GasGroupBuilder.js:112: TypeError: Cannot read property 'row' of undefined


  • Moderators

    @anuj87_ said in How to acess a subobject of dynamically created object:

    Cannot read property 'row' of undefined

    That means that your object is undefined. So your gasobject is not set / created properly.



  • @sierdzio said in How to acess a subobject of dynamically created object:

    name the subobject (objectName property), then use QObject's findChild() method
    is findChild() object method accessible from JS. I did it this way.
    Example:
    GasGroupComponent = Qt.createComponent("GasGroup.qml")
    gasGroupObject = GasGroupComponent .createObject(parent);
    var rowobject = gasGroupObject.findChild("rowest");
    if(rowobject !== null)
    console.log("Row object found in GasGroup Object");
    }

    But I am getting this error
    "qrc:/GasGroupBuilder.js:52: TypeError: Property 'findChild' of object GasGroup_QMLTYPE_0(0x2d51d840) is not a function"

    gasGroup Object is getting created as i am able to see all the visual component after object creation.



  • @anuj87_ The findChild() is not exposed for an Item in QML. So, you won't able to call an non-existence function from QML. It's a method available for an TYPE QObject object in Qt.



  • @Yashpal Thanks for you reply
    So can i accomplish the above task in qml by other means i.e
    Creating a object dynamically and then retrieve a subobject from the dynamic object .



  • @anuj87_ @sierdzio has mentioned the solutions. Please try using aliasing or use children property of parent item to get things done.



  • Thanks sierdzio and yashpal
    With your valuabl input I am able for create a soulution by adding a method "findchild( object_name)" inside gasgroup.qml
    function findchildObject( object_name)
    {
    for( var i =0 ; i<gasgroup.children.length; i++)
    {
    if(gasgroup.children[i].objectName === object_name)
    return gasgroup.children[i];
    }
    return null;
    }

    I am able to get the childobjcet now by using the children and objectName property.

    Thanks


Log in to reply