Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using QSortFilterProxyModel with a tree model

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?

like image 411
Marius Avatar asked Oct 30 '08 16:10

Marius


2 Answers

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.

like image 155
Caleb Koch Avatar answered Nov 08 '22 16:11

Caleb Koch


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);
}
like image 38
RoLi Avatar answered Nov 08 '22 18:11

RoLi