[SOLVED] Grouping and referencing dynamically created QML objects
I'm starting out with Qt Quick and there is a scenario that I'd love to hear your advice/thoughts on.
Not too far from the Toys: Dynamic Scene sample
In my case I want to be able to not only create these dynamic objects in the scene, but to also be able to iterate through them at a later point, and collect their properties. It's sort of a canvas I want the user to be able to place components on.
Can anyone help out with a direction?
You can set the "objectName" property on every created object. See a example how to iterate a tree and objects here: https://qt.gitorious.org/qt-components/qt-components/blobs/master/src/meego/Utils.js#line64
Edit: removed obviously wrong code.
Thanks for your reply. That would help me if they were already stored in some structure - but in my case I'm not sure how to store them in such a way. I can think of maintaining some array/map but I think I'm trying to bend QML to my programming concepts instead of finding out what's the QML way of doing it.
All your objects are already stored in a parent-child tree since you can't create them without parent. The first parameter of Component.createObject() is the parent.
PS: Removed the wrong code in the post above.
Ok so you mean I can use the "children" property on whichever parent I'm creating this objects for, iterate through them and use objectName to find the one I'm interested in?
I see in the documentation that the children property of Item is marked as readonly:
Does this mean I won't be able to modify data by accessing my components using this property?
[quote author="frankiefrank" date="1317030977"]Ok so you mean I can use the "children" property on whichever parent I'm creating this objects for, iterate through them and use objectName to find the one I'm interested in?[/quote]
Therefore you can change data on your child objects like you want to. Adding elements is done by creating them with the right parent and removing can be done by "myChildObject.destroy()".
Ok, thank you very much for your help and for getting me on the right track :)