[Solved] Iterate through children of a QML Item ?



  • 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 ?


  • Moderators

    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);
    }
    }
    @



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

    :-)


  • Moderators

    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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.