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?
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...
bool YourQSortFilterProxyModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
{
if (source_parent == qobject_cast<QStandardItemModel*>(sourceModel())->invisibleRootItem()->index())
{
// always accept children of rootitem, since we want to filter their children
return true;
}
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With