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:
- {
- ui->listView->setRootIndex( m_pListModel->setRootPath( kFolderinfo.filePath() ) );
- }
- ui(new Ui::MainWindow),
- m_pListModel( NULL ),
- m_pTreeModel( NULL )
- {
- ui->setupUi(this);
- ui->listView->setModel( m_pListModel );
- ui->listView->setRootIndex( kListidx );
- ui->treeView->setModel( m_pTreeModel );
- ui->treeView->setRootIsDecorated( false );
- ui->treeView->setRootIndex( kTreeidx );
- connect( ui->treeView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( OnTreeClicked( const QModelIndex& ) ) );
- }
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!
Kenny
11 replies
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 ^^:
- {
- ui->listView->setRootIndex( m_pListModel->setRootPath( m_pTreeModel->fileInfo(idx).absoluteFilePath() ) );
- }
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.
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.
Hello!!
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!!
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!
Hello again!!
Here is the code I tried following your advice.
- {
- ui.listView->setRootIndex(listModel->setRootPath(sPath));
- listModel->setFilter(origFilters);
- ui.treeView->setCurrentIndex(index);
- }
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!
Marga
You must log in to post a reply. Not a member yet? Register here!

