如何使用QSortFilterProxyModel过滤只显示子节点及其父节点的树模型?

发布于 2024-12-06 12:46:31 字数 676 浏览 4 评论 0原文

我有一个从 QAbstractItemModel 派生的工作树模型,我希望使用 QSortFilterProxyModel 子类对其进行过滤,以仅显示某些条件的子节点。例如,我有以下树:

A
- B
-- C1
-- C1
-- C1
--- C2
- D
- E

我想用节点名称 == C1 的条件来过滤此树,并仅显示具有 C1 的节点及其子节点,如下所示:

C1
C1
C1
- C2

我已经有一个重新实现了 filterAcceptsRow() 的子类可以部分完成我想要的操作,但它仍然会显示 C1 节点的父节点和祖父节点:

A
- B
-- C1
-- C1
-- C1
--- C2

我认为这是因为要考虑子节点,它们的父节点必须通过 filterAcceptsRow() 测试,对吗?如何实现 filterAcceptRows() 或其他方法,以便它可以执行我所描述的操作?

我曾在 qtcentre 和 qtforum 中问过这个问题,但没有得到任何有用的答复。我尝试直接使用 filterAcceptsRow() 内的 beginMoveRows 和 endMoveRows 移动 QSortFilterProxyModel 子类的索引,但这只会由于危险的 const_cast 而导致测试应用程序崩溃。

I have a working tree model derived from QAbstractItemModel and I wish to filter it using a QSortFilterProxyModel subclass to display only children nodes of certain criteria. For example I have the following tree:

A
- B
-- C1
-- C1
-- C1
--- C2
- D
- E

I want to filter this tree with the condition that the node has name == C1 and display only the nodes with C1 and their children like this:

C1
C1
C1
- C2

I already have a subclass with filterAcceptsRow() re-implemented that can partially do what I want but it will still show the parent and grandparent of C1 nodes:

A
- B
-- C1
-- C1
-- C1
--- C2

I think this is because for children nodes to even be considered, their parent has to pass the filterAcceptsRow() test, am I right? How can I implement filterAcceptRows() or other methods such that it can do what I have described?

I have asked this question sometime back in qtcentre and qtforum but did not get any useful reply. I tried to move the indices of the QSortFilterProxyModel subclass directly using beginMoveRows and endMoveRows inside filterAcceptsRow() but that just crashes the test application due to dangerous const_cast.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

源来凯始玺欢你 2024-12-13 12:46:31

好的,我找到了解决我的问题的方法。只需使用 QTreeView::setRootIndex() 并将索引 B 作为输入参数即可。索引 B 成为 QTreeView 的根索引,它是隐藏的,只有它的子项完整显示。

找到这个解决方案后我感到非常愚蠢。我猜我太专注于使用代理模型来修改数据的呈现方式,我完全忘记了 QTreeView。

Okay, I've found a solution to my problem. Just use QTreeView::setRootIndex() with index B as the input argument. Index B becomes the root index of the QTreeView, which is hidden and only its children are shown in full.

I felt really dumb after finding this solution. Guess I was too focused on using the proxy model to modify how the data is presented, I had totally forgotten about QTreeView.

堇年纸鸢 2024-12-13 12:46:31

我认为使用 QSortFilterProxyModel 不可能实现这一点。原因是此类仅过滤元素 - 因为它根据给定的条件隐藏(或不隐藏)某些元素。您想要做的是将树重组为新的树(从根子节点的任意位置选择元素)。这只能通过创建您自己的 QProxyModel 后代并实现自己的树重建以及在新旧树之间映射索引来实现。

准确描述如何做到这一点对于这里的答案来说有点长。

I dont think this is possible to achive using QSortFilterProxyModel. The reason for that is that this class only filters elements - menas it hides (or not) some elements, basing on given criteria. What you want to do is restructuring tree into new one (having choosen elements from arbitary position at root-children). This is only possible by creating your own QProxyModel descendant and implementing yourself tree-rebuilding, and mapping indexes between old and new tree.

Describing exactly how to do this is a bit long for an answer here.

情话已封尘 2024-12-13 12:46:31

当然 setRootIndex 是这种情况的解决方案,但如果您要寻找更复杂的模型操作,您可以考虑使用自定义代理模型,例如 http://lynxline.com/category/models/

Of course setRootIndex is the solution for this case, but if you will be looking for more complicated model manipulations you may consider using custom proxy models like http://lynxline.com/category/models/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文