Quantcast
Viewing all articles
Browse latest Browse all 4972

Exposing C++ model property of type QList to QML

Hello, sorry if this has been covered but I did try to search and could not find a solution. I am trying to use QtQuick to build user interface and work with C++ model. So far the model is as simple as this. class Axis : public QObject {     Q_OBJECT     Q_PROPERTY(QString name READ name NOTIFY nameChanged) public:     // ...     QString name() const;     // ... };   class ApplicationData : public QObject {     Q_OBJECT     Q_PROPERTY(QList<QObject*> axes READ axes NOTIFY axesChanged) public:     // ...     QList<QObject*> axes() const;     // ... private:     QList<QObject*> _axes; };   ApplicationData::ApplicationData(QObject *parent) :  QObject(parent) {     _axes.append(new Axis("Axis 1", this));     _axes.append(new Axis("Axis 2", this));     _axes.append(new Axis("Axis 3", this));     _axes.append(new Axis("Axis 4", this));     _axes.append(new Axis("Axis 5", this)); } Now I am publishing the model like this:     ApplicationData data;     engine.rootContext()->setContextProperty("appData", &data); In QML I want to display my axes list like this: ListView {     model: appData.axes     delegate: Text { text: name } } But it is not displaying. When I run the code under QtCreator, the following messages are printed. file:///.../app/Setup.qml:23: ReferenceError: name is not defined file:///.../app/Setup.qml:23: ReferenceError: name is not defined The number of messages equals to the number of objects in the object list, so it must be iterating correct object. However for some reason I does not seem to be able to access individual elements from the list. Also the following would work perfectly and display correct string: Text {     text: appData.axes[2].name } Also the ListView will display the list as expected, if I publish it like this: // C++ code engine.rootContext()->setContextProperty("axes", QVariant::fromValue(data.axes()));   // QML code ListView {     model: axes     delegate: Text { text: name } } Could anybody suggest what am I missing or how I can workaround? Thanks

Viewing all articles
Browse latest Browse all 4972

Trending Articles