0

QAbstractListModel具有说角色的实现

name, image, enabled, color, editable

比具有单一角色的实现更好(请原谅我无法想出更好的名称)

thing

这会导致QObject*带有上述Q_PROPERTYs 的 a 被返回?

在访问委托中的值时,QML 方面的区别将是一个额外的“间接”:

model.thing.name

对比

model.name

我倾向于 QObject 方法,因为它将数据视图与列表模型分开,并且具有重用的潜力。它也不需要您也实现该setData功能。据我所知,“许多角色”选项没有特定的专业人士,除非当然不可能正确定义QObject包装数据的适当子类(因为例如,对于特定情况,它在概念上几乎没有意义)。

4

3 回答 3

2

Having roles in a model is helpful when you want to use it with features that rely on roles.

For example you want to feed your model to a ComboBox, if it has roles you can just specify a textRole to the ComboBox and it will do its thing. If using a single object role, you will have to modify the delegate to display your correct object property. Roles are also needed if you want to make use of ListView's section feature.

Like @derm said, it's also useful with a QSortFilterProxyModel (shameless plug: if your model has roles, it's super easy to filter or sort it with my SortFilterProxyModel library).

You mention reusability and having to reimplement stuff. That's a good point but it's totally doable to have a generic model with roles based on QObject properties. In fact it has been done and it is available with a very permissible license here : Thomas Boutroue's QQmlObjectListModel. From c++ it has the same API as a QList but it exposes a QAbstractListModel that is usable by QML. The roles are based on the properties of your objects, and when a notify signal is emitted, it emits the corresponding dataChanged signal. insertRows and friends are also handled automatically.

With that said, I don't see much point in doing a model with a single object role. If you want a proper model with rowsInserted/rowRemoved signals (and you should, unless you just have a static model, in this case a QVariantList or a QObjectList is enough), you'd have to implement it yourself anyway when you can just use QQmlObjectListModel and be done with it.

于 2018-06-27T21:54:17.373 回答
0

使用角色意味着您必须坚持使用预定义的模型模式。

如果您的对象是低级别的,则角色很好,因此您可以将各种对象成员公开为角色。

但在QObject *模型的情况下,这是完全没有必要的。您已经在使用 Qt 的元对象系统,它可以与 QML 引擎一起促进相同的功能。

只要属性被正确实现并具有通知信号,它就可以在 QML 中无缝工作。

而且它也节省了大量时间,因为模型是通用的,您可以使用在 QML 中定义的对象填充它,而无需重新编译 C++ 代码或为每个模型都有一个专用模型。

请记住,QObjects 相当大,如果您有许多模型项,则会产生很大的开销。如果您的模型很大,或者您正在处理现有的数据集,而这些数据集不能作为QObject*开箱即用的方式使用,那么使用角色和诸如此类的东西来实现“经典”模型会更有效。

只要您不遇到它的限制,我会说单一角色QObject*模型是更简单、更容易、更快、更灵活和更优雅的解决方案。

至于提到的排序和过滤问题,库存解决方案显然不会解决它。正如这里提到的,可以实现排序和过滤,它接受一个 JS 函子,您可以在其中运行您的自定义逻辑,我敢说这实际上比基于股票角色的功能更好。同样,您可以在其中获得新代码而无需重新编译,甚至可以通过运行时生成的仿函数来驱动它。

于 2018-06-27T17:33:39.030 回答
0

是的你应该。

使用角色最根本的原因是:它是 Qt 编写模型的方式。

好的,这个原因并不重要——除非有些事情希望你以 Qt 的方式来做......

所以,你应该实现角色是有原因的,但这并不意味着你必须在 QML 中使用它们——并且取决于你打算用你的模型做什么,这将是不必要的。

在我看来,最重要的原因是,你必须实现那个dataChanged-信号。如果你不这样做,你的模型价值高达QVariantList.

对象属性的更改只会反映在生成的委托中,但依赖于dataChanged-signal 的对象不会收到更新 - 除非您在该对象的属性有任何时候为对象角色触发它改变了。
另一方面,这将对代表产生很大影响,因为必须重新评估所有绑定。

QSortFilterProxyModel- 为例,如果你没有dataChanged- 信号,它不会更新过滤器或排序,如果值已更改,因为它正在为此监听dataChanged- 信号。

特设,我不知道任何其他案例会使用 Qt 库中的那个,但可能还有其他案例。

但是,只要您不打算使用其中任何一个 - 您就不需要触发dataChanged-signal。

我不知道你是否必须真正实现角色。我没有尝试在dataChanged没有角色的情况下实现 -signal。

QSortFilterProxyModel 的代码,使用dataChanged-signal



当您不使用任何依赖于正确实现的角色或重新实现依赖于您自己的角色的功能时,您不需要这样做

于 2018-06-27T16:22:11.850 回答