How can I display only the children of a QTreeView?
I want to add a checkbox toggle to my application where a tree view gets flattened to show only the leafs nodes (child items with no children). When the toggle is unselected it should go back to the original treeview.
Something like this:
A
-B
-C
-D
-E
-F
-G
-H
-I
-K
Should become
-C
-E
-F
-K
The desired outcome should be similar to doing setIndentation(0) when the check box is toggled and setIndentation(20) when unchecked but only the children should be visible when checked and back to the original treeview when unchecked.
Other similar questions use KDE's KDescendantsProxyModel but for that you provide the depth level to show but in my case children can be at any depth level. And I am ideally looking for a solution involving only Qt APIs.
Is there a way I can use QSortProxyFilters to accomplish this?
Any thoughts/ suggestions?
Re: How can I display only the children of a QTreeView?
You can't do this with a QSortFilterProxyModel because as soon as you return false from the filterAcceptsRow() method (which you would for the row containing the parent "A", since you don't want to display it), the proxy model will ignore all children of that row. So there is no way to -not- display a parent row while displaying a subset of the children of that row.
I think you will have to implement a new tree model derived from QAbstractProxyModel as a base. In non-checked mode, this is simply an identity model which calls the tree model. In checked mode, you will have to do your filtering.
If you can find it, there used to be a FlatProxyModel in the KDE sources (files kptflatproxymodel.[h, cpp]) that flattened a tree model into a flat table model. This basically added extra columns for the parents to every leaf node, duplicating the parent information for each child row. Every level of the tree must have the same number of columns otherwise the behavior is undefined. You could probably adapt this to do what you want.