QFileSystemModel and QSortFilterProxyModel don't work well together?
Hi, I found that QSortFilterProxyModel can't filter properly when it works with QFileSystemModel. Sometimes it left NOTHING in the view. Can anyone point out where I'm wrong?
Just create a simple GUI example:
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFileSystemModel *fsm = new QFileSystemModel(this);
fsm->setRootPath(".");
sfpm->setDynamicSortFilter(true);
sfpm->setSourceModel(fsm);
sfpm
->setFilterRegExp
(QRegExp(".cpp", Qt
::CaseInsensitive,
sfpm->setFilterKeyColumn(0);
ui->tableView->setModel(sfpm);
ui->tableView->setRootIndex(sfpm->mapFromSource(fsm->index(".")));
}
if you comment out the setFilterRegExp statement, sure the files in current path are shown in the view. I've also tried setFilterWildcard and other RegExp, nothing works. Very frustrating.
Re: QFileSystemModel and QSortFilterProxyModel don't work well together?
Th doc says.
Quote:
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, "." != "your directory name".
Re: QFileSystemModel and QSortFilterProxyModel don't work well together?
this code only filter the file who name is EXACTLY ".cpp", line 12 and 13
Code:
sfpm
->setFilterRegExp
(QRegExp(".cpp", Qt
::CaseInsensitive,
QRegExp::FixedString));
this code filter all the file who suffix are "cpp",
Code:
sfpm
->setFilterRegExp
(QRegExp("*.cpp", Qt
::CaseInsensitive,
QRegExp::WildcardUnix));
that can rewrite like ...
Code:
sfpm->setFilterCaseSensitivity(Qt::CaseInsensitive);
sfpm->setFilterWildcard ("*.cpp");
Re: QFileSystemModel and QSortFilterProxyModel don't work well together?
Thanks, All. But I think spirit is basic right. It has nothing to do with if the RegExp is correct. The real problem is hierarchical data model. I need to subclass QSortFilterProxyModel to do something concerning "source_parent":
Code:
{
protected:
virtual bool MySortFilterProxyModel::filterAcceptsRow(
int source_row,
const QModelIndex &source_parent
) const{ QFileSystemModel *sm = qobject_cast<QFileSystemModel*>(sourceModel());
if (source_parent == sm->index(sm->rootPath())) {
}
return true;
}
};
This works fine for me.