QFileSystemModel filter problem!!
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:
- void MainWindow::OnTreeClicked( const QModelIndex& idx )
- QFileInfo kFolderinfo = m_pTreeModel->filePath( idx );
- ui->listView->setRootIndex( m_pListModel->setRootPath( kFolderinfo.filePath() ) );
- MainWindow::MainWindow(QWidget *parent) :
- ui(new Ui::MainWindow),
- m_pListModel( NULL ),
- m_pTreeModel( NULL )
- m_pListModel = new QFileSystemModel();
- m_pTreeModel = new QFileSystemModel();
- ui->listView->setModel( m_pListModel );
- QModelIndex kListidx = m_pListModel->setRootPath( strListFolder );
- ui->listView->setRootIndex( kListidx );
- ui->treeView->setModel( m_pTreeModel );
- ui->treeView->setRootIsDecorated( false );
- QModelIndex kTreeidx = m_pTreeModel->setRootPath( strDesktop );
- ui->treeView->setRootIndex( kTreeidx );
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!
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 ^^:
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.
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!!
Don’t know if the solution fits your needs.
The solution is a tricky one, just try it !!!
- QDir::Filters kOriFilters = Model->filter()
- View->setCurrentIndex( Model->setRootPath( setRootPath( thePath ) ) );
- Model->setFilter( the Filter different from the origin );
- Model->setFilter( kOriFilters );
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!
Here is the code I tried following your advice.
- void MainWindow::OnTreeClicked(const QModelIndex& index)
- QString sPath = treeModel->fileInfo(index).absoluteFilePath();
- QDir::Filters origFilters = listModel->filter();
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.