[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
    http://doc.qt.nokia.com/4.7-snapshot/declarative-toys-dynamicscene.html

    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.

    I realize that you can't set an ID property on dynamically created components, so I need to do something different, perhaps using a ListModel. But my QML objects can be of different types, so I'm kind of stuck on how the QML code (and related javascript) would express that.

    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.



  • Hi gri,

    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.

    You can of course store all your objects in a javascript array (or multiple to group the objects), that's no magic :)

    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:
    http://doc.qt.nokia.com/4.7-snapshot/qml-item.html#children-prop

    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]
    Right.

    The children key is readonly. This means you can not add or remove objects with javascript from it. Only the list itself is readonly, the items inside not.
    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()".

    EDIT: I also may be wrong with my description, the list object itself can not be exchanged but it might be possible to add or remove objects from it by javascript, which you should not do anyway :)



  • Ok, thank you very much for your help and for getting me on the right track :)


Log in to reply
 

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