QML + QList not picked up in setContextProperty

huangapple go评论70阅读模式
英文:

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-&gt;setContextProperty(&quot;session&quot;, &amp;session);

...

QList&lt;QObject*&gt; 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&lt;Object*&gt;,其中 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&lt;Object*&gt; 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).

huangapple
  • 本文由 发表于 2023年4月20日 00:13:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定