June 29, 2011

Kennylemon Kennylemon
Lab Rat
10 posts

QFileSystemModel filter problem!!


Hi all:
I am a newbie in Qt programming.
i’d like to make my own file manager, and using QListView, QTreeView, and two QFilesystemModel.

here is the code:

  1. void MainWindow::OnTreeClicked( const QModelIndex& idx )
  2. {
  3.     QFileInfo kFolderinfo = m_pTreeModel->filePath( idx );
  4.     ui->listView->setRootIndex( m_pListModel->setRootPath( kFolderinfo.filePath() ) );
  5. }
  7. MainWindow::MainWindow(QWidget *parent) :
  8.     QMainWindow(parent),
  9.     ui(new Ui::MainWindow),
  10.     m_pListModel( NULL ),
  11.     m_pTreeModel( NULL )
  12. {
  13.     m_pListModel = new QFileSystemModel();
  14.     m_pTreeModel = new QFileSystemModel();
  16.     QString strDesktop = QDesktopServices::storageLocation( QDesktopServices::DesktopLocation );
  17.     ui->setupUi(this);
  19.     ui->listView->setModel( m_pListModel );
  20.     m_pListModel->setFilter( QDir::NoDotAndDotDot | QDir::Files );
  21.     QString strListFolder = strDesktop + QString("/FM test/test0/");
  22.     QModelIndex kListidx = m_pListModel->setRootPath( strListFolder );    
  23.     ui->listView->setRootIndex( kListidx );
  25.     ui->treeView->setModel( m_pTreeModel );
  26.     m_pTreeModel->setFilter( QDir::NoDotAndDotDot | QDir::AllDirs );
  27.     ui->treeView->setRootIsDecorated( false );
  28.     QModelIndex kTreeidx = m_pTreeModel->setRootPath( strDesktop );
  29.     ui->treeView->setRootIndex( kTreeidx );
  31.     connect( ui->treeView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( OnTreeClicked( const QModelIndex& ) ) );
  32. }

I set the filter with “QDir::NoDotAndDotDot | QDir::Files”, and the file manager shows perfect at the beginning.
But after few steps, the filter becomes invalid.
1. Double clicked the folder called “FM test”, and it expands.
2. Choose the folder called “test0”, and the QListView shows all files without any folders in “test0”.
3. Double clicked the folder called “test0”, and it expands.
4. Choose the subfolder “testtest” of “test0”.
5. Choose back to the folder “test0”, and the filter seems become invalid, which means the QListView shows all files and the folder called “testtest”.

here is the test project, and test files included link [dl.dropbox.com]

Am I missing something??
Thanks in advanced!

11 replies

June 29, 2011

loladiro loladiro
Lab Rat
596 posts

Well, I don’t have a solution for you, I can tell you that the filter is not being invalidated, but that there is a list within QFileSystemModel that contains exceptions from the filtering and calling setRootPath adds that directory to the list, but it’s not being removed again (I don’t know whether that’s a problem with your code or with Qt). I’ll have a look at it again tonight, when I have more time.

Edit: Turns out I actually do have a workaround for you ^^:

  1. void MainWindow::OnTreeClicked( const QModelIndex& idx )
  2. {
  3.     ui->listView->setRootIndex( m_pListModel->setRootPath( m_pTreeModel->fileInfo(idx).absoluteFilePath() ) );
  4.     m_pListModel->setNameFilters(QStringList());
  5. }

If you are interested, the setNameFilters method clears that internal list I mentioned. Also, this is not the proper way to do it, but rather a dirty hack. I’ll have a look later, where the actual problem lies.

June 30, 2011

Kennylemon Kennylemon
Lab Rat
10 posts

I am really appreciate what you have done for me and for the problem.
Wow…the documentation never mention about the list inside of QFileSystemModel, really interesting.

The setNameFilters method really works for some situation, but not all of it.
Actually, I need the Namefilter to filter out some files.

Should I try to find some other ways to clear the list inside of QFileSystemModel??

Though, I am using Qt 4.7, I found out the description in Qt 4.4 like:
QModelIndex QFileSystemModel::setRootPath ( const QString & newPath )
Sets the directory that is being watched by the model to newPath. If the path is changed the model will be reset.

Is it possible that the reason of this problem is related to this sentence “If the path is changed the model will be reset.”??
What is “the model will be reset” really mean?

Thank you very much.

August 4, 2011

Kennylemon Kennylemon
Lab Rat
10 posts

It seems like that the setFilter() will not work after setRootPath(), because the filter is still the same!!!

A way to solve the problem is to change the filter after setRootPath()!!!

Is there anything much official way to solve this problem?!?

Thanks in advanced!!

September 14, 2011

Marga Marga
Lab Rat
6 posts


I’m new to Qt (as well as to the forum). I have EXACTLY the same problem described here. I have been googling for quite a while and I see other people have written about it as well. However, nobody seems to give a solution to the problem :(

If I have understood well, the QFileSystemModel is reset everytime its root path is set, right? And that is how the filters seem to get lost… But then, how can the values of the filters be restored? I have tried setting them again after the setRootPath() but the problem persists…

I would really appreciate some insight about the issue! :)) Thanks a million in advance!!

September 15, 2011

Kennylemon Kennylemon
Lab Rat
10 posts

Don’t know if the solution fits your needs.
The solution is a tricky one, just try it !!!

  1. QDir::Filters kOriFilters = Model->filter()
  2. View->setCurrentIndex( Model->setRootPath( setRootPath( thePath ) ) );
  3. Model->setFilter( the Filter different from the origin );
  4. Model->setFilter( kOriFilters );

September 15, 2011

Marga Marga
Lab Rat
6 posts

Thank you very much for your answer Kennylemon! I have tried your solution inserting this code in the slot function which is called when the user clicks on a certain folder on the tree view, but the problem seems to persist :( Did this work for you?

Now I am trying to use a QSortFilterProxyModel as the model for the QListView, but I’ve had no luck so far. I will keep you posted!

Thank you for your help!

September 16, 2011

Kennylemon Kennylemon
Lab Rat
10 posts

Hi, Marga, The solution works for me !!!
How about show us all the code u got ?!

September 16, 2011

Marga Marga
Lab Rat
6 posts

Hello again!!

Here is the code I tried following your advice.

  1. void MainWindow::OnTreeClicked(const QModelIndex& index)
  2. {
  3.      QString sPath = treeModel->fileInfo(index).absoluteFilePath();
  4.      QDir::Filters origFilters = listModel->filter();
  5.      ui.listView->setRootIndex(listModel->setRootPath(sPath));
  6.      listModel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
  7.      listModel->setFilter(origFilters);
  8.      ui.treeView->setCurrentIndex(index);
  9. }  

An this didn’t work. However, the workaround that loladiro proposed worked for me, because I don’t need to filter out any files… But there should be a better solution, right??

If you read the documentation on setFilter(), (here [doc.qt.nokia.com]) it says “Note that the filter you set should always include the QDir::AllDirs enum value, otherwise QFileSystemModel won’t be able to read the directory structure.” This makes all VERY confusing.

Thanks again!

September 18, 2011

Kennylemon Kennylemon
Lab Rat
10 posts

The snippet about filter things are almost the same as mine, and it works for mine problem!!
Maybe the problem we got is different?!

September 19, 2011

Marga Marga
Lab Rat
6 posts

I don’t know what to think, if it works for you it should work for me, right? Anyway, thanks a million for your help, I REALLY appreciate it!!


November 20, 2012

ortizjavier ortizjavier
Lab Rat
1 posts

Marga, could you solve this problem? I’m starting to think that is a Qt bug…
The given workarounds didn’t work for me :(

  ‹‹ Mixing pthreads and main GUI thread - Can pthread function emit a signal      Font for all widget ››

You must log in to post a reply. Not a member yet? Register here!