Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to acess a subobject of dynamically created object
Forum Updated to NodeBB v4.3 + New Features

How to acess a subobject of dynamically created object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 3 Posters 1.4k Views 1 Watching
  • 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 Offline
    A Offline
    anuj87_
    wrote on 15 Feb 2019, 06:38 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 15 Feb 2019, 06:57 last edited by
      #2

      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 15 Feb 2019, 07:07
      1
      • S sierdzio
        15 Feb 2019, 06:57

        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.

        A Offline
        A Offline
        anuj87_
        wrote on 15 Feb 2019, 07:07 last edited by
        #3

        @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

        S 1 Reply Last reply 15 Feb 2019, 07:34
        0
        • A anuj87_
          15 Feb 2019, 07:07

          @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

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 15 Feb 2019, 07:34 last edited by
          #4

          @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
          0
          • A Offline
            A Offline
            anuj87_
            wrote on 15 Feb 2019, 08:24 last edited by
            #5

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

            Y 1 Reply Last reply 15 Feb 2019, 08:50
            0
            • A anuj87_
              15 Feb 2019, 08:24

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

              Y Offline
              Y Offline
              Yashpal
              wrote on 15 Feb 2019, 08:50 last edited by
              #6

              @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 15 Feb 2019, 08:53
              0
              • Y Yashpal
                15 Feb 2019, 08:50

                @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 Offline
                A Offline
                anuj87_
                wrote on 15 Feb 2019, 08:53 last edited by
                #7

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

                Y 1 Reply Last reply 15 Feb 2019, 08:59
                0
                • A anuj87_
                  15 Feb 2019, 08:53

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

                  Y Offline
                  Y Offline
                  Yashpal
                  wrote on 15 Feb 2019, 08:59 last edited by
                  #8

                  @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
                  0
                  • A Offline
                    A Offline
                    anuj87_
                    wrote on 15 Feb 2019, 10:12 last edited by anuj87_
                    #9

                    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
                    0

                    1/9

                    15 Feb 2019, 06:38

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved