Auto-Accepting in QSortFilterProxyModel Since Qt 6.0, QSortFilterProxyModel Can Auto-accept Child Rows of Rows Matching the Filter.
In Qt 5.10, we added support for recursive filtering in QSortFilterProxyModel, which means keeping all parents of items matching the filter.
One of the comments in the blog post about that feature was “Sometimes, you do not only want to show parents for a match (so the match is visible), you may (also) want to show children for a match”. This is indeed something I saw a need for, more than once. For instance, you filter a large tree for a project name but then you want to see all sub-tasks of that project as well, rather than see only those that contain the project name while those that do not are hidden.
This feature has been implemented in Qt 6.0, by my colleague Giulio Camuffo (see the merge request).
Let’s use the same tree as last time, as an example:
As you can see, there is a top level item, “Using a Component in Your Application,” that contains the item “Automatic Connections,” which itself has two children.
If we simply configure our QSortFilterProxyModel with:
and filter for “Automatic,” the view shows “Automatic Connections” but not its children.
Introducing Auto-Accepting of Child Rows
If we want to keep seeing all children (and grand-children, etc.) of the rows matching the filter, all we have to do (since Qt 6.0) is add:
And here’s the result:
That’s pretty neat, too, isn’t it?
Note that auto-accepting child rows also works without recursive filtering, but then I better type a substring of one of the top level items (otherwise everything disappears) and the auto-accept feature means that all children (and grand-children, etc.) of the matching top level item(s) will still be visible.