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. [Solved] Iterate through children of a QML Item ?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Iterate through children of a QML Item ?

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 26.8k 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.
  • S Offline
    S Offline
    serdef
    wrote on last edited by
    #1

    Hi,

    i am trying to extend a qml item (in this case a flickable) with a method that logs a property of its children.
    It seems not to work, when I click, the function is executed but I get only one item and it is not one of the children.

    Application Output :
    qml: item 0
    qml: undefined
    from the code below:

    @
    import QtQuick 2.2

    ..... some qml items.....

    Flickable {
        id: flicktest
        x:0
        y:0
        width: 100
        height: 100
        contentWidth: 200
        contentHeight: 200
        function logallitems()
        {
            for (var i = 0; i < children.length; i++)
            {
                console.log("item "+i);
                console.log(children[i].propname);
            }
        }
        Text{
            id: dummy1
            x: 10
            y: 10
            property string propname :"hey1"
            text: propname + " !!"            
        }
        Text{
            id: dummy2
            x: 10
            y: 30
            property string propname :"hey2"
            text: propname + " !!"            
        }
        Text{
            id: dummy3
            x: 10
            y: 50
            property string propname :"hey3"
            text: propname + " !!"          
        }
        MouseArea{
            anchors.fill: parent
            onClicked: { flicktest.logallitems() ;}
        }
    }
    

    .... other items
    @

    Does any of you experts know how to do this the right way ?

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

      Hi,

      Try this:
      @
      function logallitems()
      {
      for (var i = 0; i < flicktest.contentItem.children.length; i++)
      {
      console.log("item "+i);
      console.log(flicktest.contentItem.children[i].propname);
      }
      }
      @

      157

      1 Reply Last reply
      0
      • S Offline
        S Offline
        serdef
        wrote on last edited by
        #3

        Thanks for the fast response p3c0,
        that works well!!

        :-)

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

          You are Welcome :)
          contentItem works because all the items in Flickable are actually childrens of it.
          More "here":http://qt-project.org/doc/qt-5/qml-qtquick-flickable.html#contentItem-prop

          157

          1 Reply Last reply
          0
          • S Offline
            S Offline
            serdef
            wrote on last edited by
            #5

            Thanks for explaining, now I also understand why, what a luxury !!
            :-)

            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