Skip to content

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: Complete tree

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.

Result after recursive filtering

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: Result after auto-accepting child rows

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.

About KDAB

If you like this article and want to read similar material, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

FacebookTwitterLinkedInEmail
Leave a Reply

Your email address will not be published.