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. Access the properties of a child of a component

Access the properties of a child of a component

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 7.7k Views
  • 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.
  • saitejS Offline
    saitejS Offline
    saitej
    wrote on last edited by
    #1

    I have a component in qml and a rectangle as its child. How do I access the properties of that rectangle. If I try to access via id, I get an reference error. After some search I found that we need to use alias for the those properties. So I have changed the code to the following.

    Component{
    id:selectComponent
    Rectangle {
                id: selComp
    property alias width_rect: selComp.width
    
    }
    }
    
    
    
    Button{
    
    ....
     onClicked: {
    
                    console.log(selectionComponent.width_rect)
                }
    }
    

    I stiil get an undefined error. How do I access the properties of the child rectangle?

    Thanks!!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      move the property alias outside the rectagle.

      Component{
      id:selectComponent
      Rectangle {
                  id: selComp
      }
      property alias width_rect: selComp.width
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      saitejS 1 Reply Last reply
      0
      • K Offline
        K Offline
        kumar90
        wrote on last edited by
        #3

        you should access the property via the property of that item , i.e selectionComponent.selComp.width_rect or selComp.width_rect .

        saitejS 1 Reply Last reply
        0
        • VRoninV VRonin

          move the property alias outside the rectagle.

          Component{
          id:selectComponent
          Rectangle {
                      id: selComp
          }
          property alias width_rect: selComp.width
          }
          
          saitejS Offline
          saitejS Offline
          saitej
          wrote on last edited by
          #4

          @VRonin

          If I keep it outside the rectangle, I get this error.

          Component objects cannot declare new properties.

          1 Reply Last reply
          1
          • K kumar90

            you should access the property via the property of that item , i.e selectionComponent.selComp.width_rect or selComp.width_rect .

            saitejS Offline
            saitejS Offline
            saitej
            wrote on last edited by
            #5

            @kumar90 said in Access the properties of a child of a component:

            selectionComponent.selComp.width_rect

            For this I get the error:

            TypeError: Cannot read property 'width_rect' of undefined

            Basically, It can't access the id of the child so it gives the undefined error!!

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @saitej Component needs to be loaded first either by Loader or using createObject.

              157

              saitejS 1 Reply Last reply
              0
              • p3c0P p3c0

                @saitej Component needs to be loaded first either by Loader or using createObject.

                saitejS Offline
                saitejS Offline
                saitej
                wrote on last edited by saitej
                #7

                @p3c0

                Ya ... I am already creating the object on a mouse click event.

                selection = selectComponent.createObject(parent, {"x": parent.width / 4, "y": parent.height / 4, "width": parent.width / 2, "height": parent.width / 2})
                

                I got it now. I was accessing via selectComponent rather than selection (object )

                Thanks

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @saitej
                  Then you need to access or set the properties by selection object and not selectComponent.

                  157

                  saitejS 1 Reply Last reply
                  0
                  • p3c0P p3c0

                    @saitej
                    Then you need to access or set the properties by selection object and not selectComponent.

                    saitejS Offline
                    saitejS Offline
                    saitej
                    wrote on last edited by
                    #9

                    @p3c0

                    Ya I jus realised my mistake.

                    Thanks a lot!!

                    1 Reply Last reply
                    0

                    • Login

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