英文:
QML + QList<QObject*> not picked up in setContextProperty
问题
I'm attempting to do something akin to Using C++ Models with Qt Quick Views . 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 Session object 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)
...
};
Each of its items is a DataObject
which has a name property
Then effectively we do a:
ListView {
model: session.items
delegate: Rectangle {
required property string name
}
}
and in main:
Session session;
QQmlContext* context = engine.rootContext();
context->setContextProperty("session", &session);
...
QList<QObject*> dataList;
session.setItems(dataList);
Everything yields a:
Required property was not initialized
despite verifying there is content in session.items
. I use this kind of Session object for other QML things without incident.
What am I doing wrong?
EDIT:
As mention in comments, one can get to properties via modelData
which is a functional workaround, but the initial question remains.
I didn't put the QML in here because it's kinda big, but here's a gist of it in its current form link to gist
英文:
I'm attempting to do something akin to Using C++ Models with Qt Quick Views . 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 Session object 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)
...
};
Each of its items is a DataObject
which has a name property
Then effectively we do a:
ListView {
model: session.items
delegate: Rectangle {
required property string name
}
}
and in main:
Session session;
QQmlContext* context = engine.rootContext();
context->setContextProperty("session", &session);
...
QList<QObject*> dataList;
session.setItems(dataList);
Everything yields a:
Required property was not initialized
despite verifying there is content in session.items
. I use this kind of Session object for other QML things without incident.
What am I doing wrong?
EDIT:
As mention in comments, one can get to properties via modelData
which is a functional workaround, but the initial question remains.
I didn't put the QML in here because it's kinda big, but here's a gist of it in its current form https://gist.github.com/malachib/ac05c535fd11c5d4961feade307d9102
答案1
得分: 1
根据文档的说法:
在模型-视图-委托(model-view-delegate)代码中,必需属性起到特殊作用:**如果视图的委托具有与视图模型的角色名称匹配的必需属性,则这些属性将用模型的相应值进行初始化。**有关更多信息,请访问 Qt Quick 中的模型和视图页面。
因此,如果您有一个自定义列表,类型为 QList<Object*>
,其中 QObject
是您的自定义类(而不是继承自 QAbstractItemModel 的类,因为必需的值将在重写的 rolenames 函数中定义为相同名称时进行初始化,例如 {"SomeRole", "someRole"} - 必需属性字符串 someRole),则委托中定义的必需属性将不会使用您的 自定义类
属性的默认值进行初始化(您也不能在定义它们时直接设置它们的值)。
英文:
As documentation says:
Required properties play a special role in model-view-delegate code: If the delegate of a view has required properties whose names match with the role names of the view's model, then those properties will be initialized with the model's corresponding values. For more information, visit the Models and Views in Qt Quick page.
So, if you have custom list in type of QList<Object*>
where QObject
is your object derived custom class here(instead of QAbstractItemModel derived one since the required values will be initalized as soon as they defined with the same name in the overridden rolenames function e.g. {"SomeRole", "someRole"} - required property string someRole), the defined required properties in the delegate will not be initialized with the default values of your custom class
properties(also you can not directly set their values when defining them).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论