Results 1 to 4 of 4

Thread: QFileSystemModel and QSortFilterProxyModel don't work well together?

  1. #1

    Question 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:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. QFileSystemModel *fsm = new QFileSystemModel(this);
    7. fsm->setRootPath(".");
    8.  
    9. sfpm->setDynamicSortFilter(true);
    10. sfpm->setSourceModel(fsm);
    11. sfpm->setFilterRegExp(QRegExp(".cpp", Qt::CaseInsensitive,
    12. QRegExp::FixedString));
    13. sfpm->setFilterKeyColumn(0);
    14.  
    15. ui->tableView->setModel(sfpm);
    16.  
    17. ui->tableView->setRootIndex(sfpm->mapFromSource(fsm->index(".")));
    18.  
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFileSystemModel and QSortFilterProxyModel don't work well together?

    Th doc says.
    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".
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemModel and QSortFilterProxyModel don't work well together?

    this code only filter the file who name is EXACTLY ".cpp", line 12 and 13
    Qt Code:
    1. sfpm->setFilterRegExp(QRegExp(".cpp", Qt::CaseInsensitive,QRegExp::FixedString));
    To copy to clipboard, switch view to plain text mode 

    this code filter all the file who suffix are "cpp",
    Qt Code:
    1. sfpm->setFilterRegExp(QRegExp("*.cpp", Qt::CaseInsensitive,QRegExp::WildcardUnix));
    To copy to clipboard, switch view to plain text mode 

    that can rewrite like ...
    Qt Code:
    1. sfpm->setFilterCaseSensitivity(Qt::CaseInsensitive);
    2. sfpm->setFilterWildcard ("*.cpp");
    To copy to clipboard, switch view to plain text mode 
    Last edited by ecanela; 21st August 2012 at 23:05. Reason: spelling corrections

  4. #4

    Wink 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":

    Qt Code:
    1. class MySortFilterProxyModel : public QSortFilterProxyModel
    2. {
    3. protected:
    4. virtual bool MySortFilterProxyModel::filterAcceptsRow(
    5. int source_row, const QModelIndex &source_parent) const{
    6. QFileSystemModel *sm = qobject_cast<QFileSystemModel*>(sourceModel());
    7. if (source_parent == sm->index(sm->rootPath())) {
    8. return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
    9. }
    10. return true;
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 
    This works fine for me.

Similar Threads

  1. Replies: 0
    Last Post: 9th March 2011, 00:08
  2. Help with QFileSystemModel
    By TheShow in forum Qt Programming
    Replies: 4
    Last Post: 5th January 2010, 20:11
  3. Replies: 12
    Last Post: 5th July 2009, 16:03
  4. Replies: 6
    Last Post: 3rd November 2006, 11:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.