“分组依据"代理模式 [英] "group by" proxy model

查看:27
本文介绍了“分组依据"代理模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有理论上无限深度的树模型和一些属性组".

I have tree model with theoretically infinite depth and some property "Group".

除了标准视图之外,我还需要以这样的方式显示此模型(并保持同步),使每个组成为具有相同属性值的所有项目的虚拟父项.

In addition to standard view, I need to show this model (and keep it in sync) in such way that each group becomes a virtual parent for all items with the same property value.

使用 Qt 的模型/视图架构实现这一目标的最佳方法是什么?

What is the best way to achieve this with Qt's model/view architecture?

不久前我通过在模型中添加/删除项目时插入额外的虚拟组"解决了类似的问题,但事实证明这种方法不是很灵活,所以我正在寻找更好的解决方案.

I had solved similar problem not a long time ago via inserting additional virtual "groups" on adding/removing items to/from the model, but this method proved not to be very flexible so I'm looking for better solution.

理想情况下,我现在看到这是通过 QSortProxyFilter 模型实现的.

Ideally, I see this implemented via QSortProxyFilter model as for now.

推荐答案

取决于你想要的最终结果:

Depending on what you want as a final result :

  • 无需实现 QAbstractProxyModel 即可实现过滤.

  • you can achieve the filtering without having to implement QAbstractProxyModel.

QSortFilterProxyModel 可以按 Item role 排序,默认为 Qt::DisplayRole.但是,如果您将组属性放在项目的自定义角色中,则可以按此自定义角色排序:cf:https://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop

The QSortFilterProxyModel can sort by Item role , default being Qt::DisplayRole. But if you put your group property in a custom Role of your item, then you can sort by this custom role : cf : https://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop

这应该完成按组排序的特定视图的主要排序工作.

That should do the main job of sorting for the specific view ordered by Group.

但是你不会有可折叠组"节点.很难说您是否可以使用自定义渲染器管理等效功能.

But you won't have "collapsable group" node. hard to say if you can manage to equivalent feature with a custom renderer.

否则,如 Felix 建议的那样,实现您的自定义 QAbstractProxyModel 将创建节点组索引,这是一种艰难的方法.

Otherwise, the hard way, as suggested by Felix, implement your custom QAbstractProxyModel that will create node group indexes.

最后一个解决方案:创建您自己的(基本)模型(类似于 MyTree 和 Map 和两个 Qt 模型(每个视图一个).当您的基本模型更新时,通过 Qt 的信号/槽机制更新两个模型,反之亦然.

Last solution : create your own (base) model (something like MyTree and Map<Group,Node> , and two Qt models (one for each view)). Update both models via Qt's signal/slot mecanism when your base model is updated and vice versa.

这篇关于“分组依据"代理模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆