How to display a QAbstractListModel in qml?
-
I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.
// main.cpp #include <QAbstractListModel> #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> struct Model : QAbstractListModel { QStringList lst{"txt1", "txt2", "txt3"}; int rowCount(const QModelIndex &parent) const override { // never called if (parent.isValid()) return 0; return lst.length(); } QVariant data(const QModelIndex &index, int role) const override { // never called if (index.row() < 0 || index.row() >= lst.size()) return QVariant{}; if (role == Qt::DisplayRole) return lst.at(index.row()); return QVariant{}; } }; int main(int argc, char *argv[]) { QGuiApplication app{argc, argv}; Model model; QQmlApplicationEngine engine; QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); QQmlContext *context = engine.rootContext(); context->setContextProperty("model", &model); // doesn't work: engine.setInitialProperties({{"model", // QVariant::fromValue(&model)}}); engine.loadFromModule("app", "Main"); return app.exec(); }
import QtQuick import QtQuick.Controls ApplicationWindow { visible: true width: 640 height: 480 title: "App" ListView { id: listView anchors.fill: parent model: model delegate: Item { width: listView.width height: 50 Text { anchors.centerIn: parent text: model.display } } } }
-
@jeremy_k said in How to display a QAbstractListModel in qml?:
@mjs225 said in How to display a QAbstractListModel in qml?:
I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.
// main.cpp // doesn't work: engine.setInitialProperties({{"model", // QVariant::fromValue(&model)}});
The top level component doesn't have a
model
property, and setInitialProperties doesn't recursively search subcomponents to apply to.ApplicationWindow { ListView { id: listView anchors.fill: parent model: model } }
The right side of
model: model
within listView is going to resolve tolistView.model
, ie itself.Thanks. I find 2 fixes based on the suggestion:
- Rename the property "model".
in C++:context->setContextProperty("mymodel", &model);
in qml:ListView { model: mymodel }
- in C++: replace
context->setContextProperty("model", &model);
withengine.setInitialProperties({{"model", QVariant::fromValue(&model)}});
in qml: Addproperty var model
andid: appWindow
to top level component. Usemodel: appWindow.model
in ListView
- Rename the property "model".
-
@mjs225 said in How to display a QAbstractListModel in qml?:
I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.
// main.cpp // doesn't work: engine.setInitialProperties({{"model", // QVariant::fromValue(&model)}});
The top level component doesn't have a
model
property, and setInitialProperties doesn't recursively search subcomponents to apply to.ApplicationWindow { ListView { id: listView anchors.fill: parent model: model } }
The right side of
model: model
within listView is going to resolve tolistView.model
, ie itself. -
@jeremy_k said in How to display a QAbstractListModel in qml?:
@mjs225 said in How to display a QAbstractListModel in qml?:
I want to display a QAbstractListModel in qml. I create a simple app but fails to make it work. The window is empty and the model is not accessed from qml. Please help point out what's wrong with this code.
// main.cpp // doesn't work: engine.setInitialProperties({{"model", // QVariant::fromValue(&model)}});
The top level component doesn't have a
model
property, and setInitialProperties doesn't recursively search subcomponents to apply to.ApplicationWindow { ListView { id: listView anchors.fill: parent model: model } }
The right side of
model: model
within listView is going to resolve tolistView.model
, ie itself.Thanks. I find 2 fixes based on the suggestion:
- Rename the property "model".
in C++:context->setContextProperty("mymodel", &model);
in qml:ListView { model: mymodel }
- in C++: replace
context->setContextProperty("model", &model);
withengine.setInitialProperties({{"model", QVariant::fromValue(&model)}});
in qml: Addproperty var model
andid: appWindow
to top level component. Usemodel: appWindow.model
in ListView
- Rename the property "model".
-