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



  • 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.



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



  • 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.



  • 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.


Log in to reply
 

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