QML + QList<QObject*> won't work with setContextProperty
-
I'm attempting to do something akin to https://doc.qt.io/qt-6/qtquick-modelviewsdata-cppmodels.html . Specifically I want a QList of QObject-derived instances to render in a QML ListView.
If I follow the examples exactly, it works. However, if I attempt to acquire the model through a custom
Sessionobject set to context, I run into issues.Specifically, the session code looks like:
class Session : public QObject { Q_OBJECT typedef QVariant result_type; Q_PROPERTY(result_type items READ items NOTIFY itemsChanged) ... };Then effectively we do a:
ListView { model: session.items }and in main:
Session session; context->setContextProperty("session", &session);I've spent around 4 hours on it now. Everything yields a "Required property was not initialized" despite verifying there is content in
session.itemsI'm happy to expand the example or feel free to browse full proof of concept code here https://bitbucket.org/malachib/playground.qt/src/master/src/PGQT-43/
-
I have observed that if the consuming QML uses
modelDatarather than properties, things work as expected.i.e.
delegate: RowLayout { required property string name Text { Layout.fillWidth: true; text: parent.name } }exhibits the problem at hand, while
delegate: RowLayout { Text { Layout.fillWidth: true; text: modelData.name } }Works.
According to the documentation, we expect the property version to work as well: "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"
So, the problem is still unsolved.