[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 ?
-
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!!:-)
-
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 !!
:-)