Using custom C++ models in QML



  • Hello,

    We use QML to draw the 2D user interface of a 3D software.

    We are building models in C++, that we send to QML, to use in ListViews for example.
    The models inherit from QAbstractListModel.

    The problem is we cannot use the common accessors from the ListModel encapsulated in the ListView, because there is no interface provinding those method declarations in C++.

    What is the common and proper way to define our own models in C++ so that we can use them in QML for both reading and writing?

    Thanks !



  • Hello,

    The only thing I found in documentation is "this":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html#accessing-views-and-models-from-delegates . Look at the end of page.



  • Hi,

    In general you will need to add your own slots (or Q_INVOKABLE functions) for any functionality not provided out of the box by QAbstractListModel. You could, for example, add a setter like ListModel

    @ Q_INVOKABLE void setProperty(int index, const QString& property, const QVariant& value);@

    and implement the appropriate logic for your model.

    Regards,
    Michael



  • Hi,

    Thanks for the answers guys, it confirmed our thoughts!

    Finally, what we will do is to implement the ListModel methods within our custom models, so that we will be able to use both the standard ListModels and our custom models in our custom QML components, or the standard ListView and other StuffView components.

    We think it is the more sustainable and generic solution within our range of possibilities...
    Any counter-arguments? :-)

    Regards,

    Jérémie


Log in to reply
 

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