Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved How to acess a subobject of dynamically created object

    QML and Qt Quick
    3
    9
    804
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      anuj87_ last edited by

      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 ?

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        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.

        (Z(:^

        A 1 Reply Last reply Reply Quote 1
        • A
          anuj87_ @sierdzio last edited by

          @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

          sierdzio 1 Reply Last reply Reply Quote 0
          • sierdzio
            sierdzio Moderators @anuj87_ last edited by

            @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.

            (Z(:^

            1 Reply Last reply Reply Quote 0
            • A
              anuj87_ last edited by

              @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.

              Yashpal 1 Reply Last reply Reply Quote 0
              • Yashpal
                Yashpal @anuj87_ last edited by

                @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.

                A 1 Reply Last reply Reply Quote 0
                • A
                  anuj87_ @Yashpal last edited by

                  @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 .

                  Yashpal 1 Reply Last reply Reply Quote 0
                  • Yashpal
                    Yashpal @anuj87_ last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • A
                      anuj87_ last edited by anuj87_

                      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

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post