I have a QDirModel
whose current directory is set. Then I have a QListView
which is supposed to show the files in that directory. This works fine.
Now I want to limit the files shown, so it only shows png files (the filename ends with .png). The problem is that using a QSortFilterProxyModel
and setting the filter regexp will try to match every parent of the files as well. According to the documentation:
For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown.
So, how do I get the QSortFilterProxyModel
to only filter the files in the directory, and not the directories it resides in?
Just use KRecursiveFilterProxyModel model from the KItemModels KDE API
We ran into something similar where I work, and ended up making our own proxy model to do our filtering. However, looking through the documentation for what you want (which seems like it would be a more common case), I came across two possibilities.
filterAcceptsRow
function. From the documentation:Then you could presumably use the model index to check if the index item is a directory (automatically accept) or a file (filter on filename).
For people like me who are interested in the following behaviour : if a child matches the filter, then its ancestors should not be hidden :
As of Qt 5.10,
QSortFilterProxyModel
has the option to filter recursively. In other words, if a child matches the filter, its parents will be visible as well.Check out QSortFilterProxyModel::recursiveFilteringEnabled.
derive qsortfilterproxymodel and then...