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. Exposing a hierarchical data model/object to QML with bindings.

Exposing a hierarchical data model/object to QML with bindings.

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 3.3k 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.
  • S Offline
    S Offline
    scubakid
    wrote on last edited by
    #1

    Can anyone explain a good way to expose a hierarchical data model/object to QML?

    I have looked at all the ways and I am not convinced there is a good way of doing this?

    I am looking for a way to implement bindings to the data (in other words be able for the QML to pick up changes to a data model from cpp), which will need to be a flexible hierarchical data model.

    The problem I seem to encounter all the time is the nature of QObject's and the fact they cannot be copied or assigned. as discussed here http://qt-project.org/doc/qt-4.8/object.html

    so I cannot expose my own class inherited from QObject as a property using the macro Q_PROPERTY()

    I want to avoid using exposed properties as QLists and QMaps as they require manually updating the model when they change with a new setContextProperty call and you cannot really bind to elements of them from within QML.

    I cannot register a type with QML as this would need to be a QQuickItem/QDeclarativeItem and this is a drawing item.

    Is there a way of doing this with an AbstractItemModel (or equivalent) that I cannot realize?

    any help/insight would be great.

    cheers.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rcari
      wrote on last edited by
      #2

      This is feasible with the Model/View framework in Qt. See the "documentation":http://qt-project.org/doc/qt-4.8/qdeclarativemodels.html.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        scubakid
        wrote on last edited by
        #3

        Thanks for you reply.

        However I don't see my solution in this documentation, I have been through this before, unless I missed something?

        The QAbstractListModel uses flat array structures, so my question is how can I add another layer (or layers) to this model and keep the dynamic binding to the QML properties?

        For instance, if I wanted to expose hierarchical data structures (directory style) with bindings to each parent and child within that data structure.

        a simple example being...
        @
        Fruit {
        Orange [ {Name = "Satsuma", Color = "Orange", Quantity=12} , {Name = "Valencia", Color = "Orange", Quantity=22} ]
        ...
        }
        @
        I want to expose the whole data structure and to bind say Fruit.Orange[ 0 ].name to a Text property in QML so that when the entry name changes, the bound Text property also updates.

        Without having to announce a change to the entire structure.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          scubakid
          wrote on last edited by
          #4

          Ok, turns out I can do this using nested a models.

          For anyone else interested here is a good example:

          http://cdumez.blogspot.co.uk/2010/12/expose-nested-c-models-to-qml.html

          Cheers.

          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