“group by” proxy model

北慕城南 提交于 2020-01-02 08:28: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.

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.

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


回答1:


Depending on what you want as a final result :

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

    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.

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

  • 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.



来源:https://stackoverflow.com/questions/49759442/group-by-proxy-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!