Exposing C++ ModelsModels can be defined in C++ and then made available to QML. This is useful for exposing existing C++ data models or otherwise complex datasets to QML. A C++ model class can be defined as a QStringList, a QList<QObject*> or a QAbstractItemModel. The first two are useful for exposing simpler datasets, while QAbstractItemModel provides a more flexible solution for more complex models. QStringList-based ModelA model may be a simple QStringList, which provides the contents of the list via the modelData role. Here is a ListView with a delegate that references its model item's value using the modelData role: ListView { width: 100; height: 100 model: myModel delegate: Rectangle { height: 25 width: 100 Text { text: modelData } } } A Qt application can load this QML document and set the value of myModel to a QStringList: QStringList dataList; dataList.append("Item 1"); dataList.append("Item 2"); dataList.append("Item 3"); dataList.append("Item 4"); QQuickView view; QQmlContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); The complete example is available in Qt's examples/quick/modelviews/stringlistmodel directory. Note: There is no way for the view to know that the contents of a QStringList have changed. If the QStringList changes, it will be necessary to reset the model by calling QQmlContext::setContextProperty() again. QObjectList-based modelA list of QObject* values can also be used as a model. A QList<QObject*> provides the properties of the objects in the list as roles. The following application creates a DataObject class that with Q_PROPERTY values that will be accessible as named roles when a QList<DataObject*> is exposed to QML: class DataObject : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged) ... }; int main(int argc, char ** argv) { QGuiApplication app(argc, argv); QList<QObject*> dataList; dataList.append(new DataObject("Item 1", "red")); dataList.append(new DataObject("Item 2", "green")); dataList.append(new DataObject("Item 3", "blue")); dataList.append(new DataObject("Item 4", "yellow")); QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); QQmlContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); ... The QObject* is available as the modelData property. As a convenience, the properties of the object are also made available directly in the delegate's context. Here, view.qml references the DataModel properties in the ListView delegate: ListView { width: 100; height: 100 model: myModel delegate: Rectangle { height: 25 width: 100 color: model.modelData.color Text { text: name } } } Note the use of the fully qualified access to the color property. The properties of the object are not replicated in the model object, since they are easily available via the modelData object. The complete example is available in Qt's examples/quick/modelviews/objectlistmodel directory. Note: There is no way for the view to know that the contents of a QList have changed. If the QList changes, it will be necessary to reset the model by calling QQmlContext::setContextProperty() again. QAbstractItemModelA model can be defined by subclassing QAbstractItemModel. This is the best approach if you have a more complex model that cannot be supported by the other approaches. A QAbstractItemModel can also automatically notify a QML view when the model data has changed. The roles of a QAbstractItemModel subclass can be exposed to QML by calling QAbstractItemModel::setRoleNames(). The default role names set by Qt are:
Here is an application with a QAbstractListModel subclass named AnimalModel that has type and size roles. It calls QAbstractItemModel::setRoleNames() to set the role names for accessing the properties via QML: class Animal { public: Animal(const QString &type, const QString &size); ... }; class AnimalModel : public QAbstractListModel { Q_OBJECT public: enum AnimalRoles { TypeRole = Qt::UserRole + 1, SizeRole }; AnimalModel(QObject *parent = 0); ... }; AnimalModel::AnimalModel(QObject *parent) : QAbstractListModel(parent) { QHash<int, QByteArray> roles; roles[TypeRole] = "type"; roles[SizeRole] = "size"; setRoleNames(roles); } int main(int argc, char ** argv) { QGuiApplication app(argc, argv); AnimalModel model; model.addAnimal(Animal("Wolf", "Medium")); model.addAnimal(Animal("Polar bear", "Large")); model.addAnimal(Animal("Quoll", "Small")); QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); QQmlContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", &model); ... This model is displayed by a ListView delegate that accesses the type and size roles: ListView { width: 200; height: 250 model: myModel delegate: Text { text: "Animal: " + type + ", " + size } } QML views are automatically updated when the model changes. Remember the model must follow the standard rules for model changes and notify the view when the model has changed by using QAbstractItemModel::dataChanged(), QAbstractItemModel::beginInsertRows(), etc. See the Model subclassing reference for more information. The complete example is available in Qt's examples/quick/modelviews/abstractitemmodel directory. QAbstractItemModel presents a hierarchy of tables, but the views currently provided by QML can only display list data. In order to display child lists of a hierarchical model the VisualDataModel element provides several properties and functions for use with models of type QAbstractItemModel:
Exposing C++ Data Models to QMLThe above examples use QQmlContext::setContextProperty() to set model values directly in QML components. An alternative to this is to register the C++ model class as a QML type from a QML C++ plugin using QQmlExtensionPlugin. This would allow the model classes to be created directly as elements within QML:
See Tutorial: Writing QML extensions with C++ for details on writing QML C++ plugins. |