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 access item's children
Forum Updated to NodeBB v4.3 + New Features

How to access item's children

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 6 Posters 8.5k 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.
  • Y Offline
    Y Offline
    ytbryan
    wrote on last edited by
    #1

    Hi,

    For instance, the rect.color. How do I access it?

    Thanks!

    @
    //SimpleRect.qml
    Item
    {
    width:100
    height:100

    Rectangle
    {
    id:Rect
    anchors.fill: parent
    color: "green"

                }
    

    }
    @

    1 Reply Last reply
    0
    • U Offline
      U Offline
      ucomesdag
      wrote on last edited by
      #2

      From the "doc":http://developer.qt.nokia.com/doc/qt-4.7/qdeclarativeintroduction.html#id-0072e7d8-628c-49ee-810f-35f1b672c46f:
      [quote]Note that an id must begin with a lower-case letter or an underscore, and cannot contain characters other than letters, numbers and underscores.[/quote]

      @//SimpleRect.qml
      Item {
      width:100
      height:100

      Rectangle {
      id: myrectangle
      anchors.fill: parent
      color: "green"
      }

      }@
      and you acces it with myrectangle.color

      Write “Qt”, not “QT” (QuickTime).

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        ytbryan
        wrote on last edited by
        #3

        Thanks for the reply.

        What if I want to use SimpleRect in another qml? How do I access the color property then?

        @

        Rectangle
        {
        color: "pink"
        width:500
        height:500

        SimpleRect
        {
        width:100
        height:100
        x: 200
        y:200
        rect.color : "blue" // How do I access the color then?
        }

        }@

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MichK
          wrote on last edited by
          #4

          You have to make alias property or bind parent property with child property:

          Alias:
          @
          //SimpleRect.qml
          Item {
          id: root
          ...
          property alias childColor: myrectangle.color

          Rectangle {
          id: myrectangle
          ...
          }
          }
          @

          Binding:
          @
          //SimpleRect.qml
          Item {
          id: root
          ...
          property color childColor

          Rectangle {
          id: myrectangle
          ...
          color: root.childColor
          }
          }
          @

          Usage in both cases:
          @
          //OtherFile.qml
          SimpleRect {
          id: simple
          ...
          childColor: "blue"
          }
          @

          N 1 Reply Last reply
          1
          • T Offline
            T Offline
            thisisbhaskar
            wrote on last edited by
            #5

            [quote author="michal.k" date="1312787834"]

            Binding:
            @
            //SimpleRect.qml
            Item {
            id: root
            ...
            property color childColor

            Rectangle {
            id: myrectangle
            ...
            color: root.childColor
            }
            }
            @[/quote]

            The best is use alias, not binding. With above code, when you change color of myrectangle, childColor is not updated.

            Alias resolves this problem.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              amaklin6
              wrote on last edited by
              #6

              Good. this helped me as well

              1 Reply Last reply
              0
              • M MichK

                You have to make alias property or bind parent property with child property:

                Alias:
                @
                //SimpleRect.qml
                Item {
                id: root
                ...
                property alias childColor: myrectangle.color

                Rectangle {
                id: myrectangle
                ...
                }
                }
                @

                Binding:
                @
                //SimpleRect.qml
                Item {
                id: root
                ...
                property color childColor

                Rectangle {
                id: myrectangle
                ...
                color: root.childColor
                }
                }
                @

                Usage in both cases:
                @
                //OtherFile.qml
                SimpleRect {
                id: simple
                ...
                childColor: "blue"
                }
                @

                N Offline
                N Offline
                nandhishtr
                wrote on last edited by
                #7

                @MichK Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                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