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] Grouping and referencing dynamically created QML objects

[SOLVED] Grouping and referencing dynamically created QML objects

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 2 Posters 4.5k Views
  • 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.
  • F Offline
    F Offline
    frankiefrank
    wrote on last edited by
    #1

    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?

    "Roads? Where we're going, we don't need roads."

    1 Reply Last reply
    0
    • T Offline
      T Offline
      theoriginalgri
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frankiefrank
        wrote on last edited by
        #3

        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.

        "Roads? Where we're going, we don't need roads."

        1 Reply Last reply
        0
        • T Offline
          T Offline
          theoriginalgri
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            frankiefrank
            wrote on last edited by
            #5

            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?

            "Roads? Where we're going, we don't need roads."

            1 Reply Last reply
            0
            • T Offline
              T Offline
              theoriginalgri
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • F Offline
                F Offline
                frankiefrank
                wrote on last edited by
                #7

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

                "Roads? Where we're going, we don't need roads."

                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