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:
proxy->setRecursiveFilteringEnabled(true);
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:
proxy->setAutoAcceptChildRows(true);
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.
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications. In addition to being leading experts in Qt, C++ and 3D technologies for over two decades, KDAB provides deep expertise across the stack, including Linux, Rust and modern UI frameworks. With 100+ employees from 20 countries and offices in Sweden, Germany, USA, France and UK, we serve clients around the world.
David Faure
Senior Software Engineer
Senior software engineer and Managing Director of KDAB’s French office, David is a Qt user since its beginning. He has made numerous contributions to Qt, including new classes for QtCore in Qt 5. David is well known in the KDE project for his work on the web browser and especially on KDE Frameworks. He has become a specialist in multithreading with Qt, as well as performance optimizations. David holds an MSc in Computer Science.