PDA

View Full Version : QFileSystemModel and QSortFilterProxyModel don't work well together?



solotim
21st August 2012, 11:32
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:


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFileSystemModel *fsm = new QFileSystemModel(this);
fsm->setRootPath(".");

QSortFilterProxyModel *sfpm = new QSortFilterProxyModel();
sfpm->setDynamicSortFilter(true);
sfpm->setSourceModel(fsm);
sfpm->setFilterRegExp(QRegExp(".cpp", Qt::CaseInsensitive,
QRegExp::FixedString));
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.

spirit
21st August 2012, 12:07
Th doc says (http://qt-project.org/doc/qt-4.8/qsortfilterproxymodel.html#filtering).

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".

ecanela
21st August 2012, 22:48
this code only filter the file who name is EXACTLY ".cpp", line 12 and 13

sfpm->setFilterRegExp(QRegExp(".cpp", Qt::CaseInsensitive,QRegExp::FixedString));

this code filter all the file who suffix are "cpp",

sfpm->setFilterRegExp(QRegExp("*.cpp", Qt::CaseInsensitive,QRegExp::WildcardUnix));

that can rewrite like ...


sfpm->setFilterCaseSensitivity(Qt::CaseInsensitive);
sfpm->setFilterWildcard ("*.cpp");

solotim
22nd August 2012, 03:32
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":


class MySortFilterProxyModel : public QSortFilterProxyModel
{
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 QSortFilterProxyModel::filterAcceptsRow(source_row , source_parent);
}
return true;
}
};

This works fine for me.