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) :
m_pListModel( NULL ),
m_pTreeModel( NULL )
m_pListModel = new QFileSystemModel();
m_pTreeModel = new QFileSystemModel();
QString strDesktop = QDesktopServices::storageLocation( QDesktopServices::DesktopLocation ); ui->setupUi(this); ui->listView->setModel( m_pListModel ); m_pListModel->setFilter( QDir::NoDotAndDotDot | QDir::Files ); QString strListFolder = strDesktop + QString("/FM test/test0/"); QModelIndex kListidx = m_pListModel->setRootPath( strListFolder ); ui->listView->setRootIndex( kListidx ); ui->treeView->setModel( m_pTreeModel ); m_pTreeModel->setFilter( QDir::NoDotAndDotDot | QDir::AllDirs ); ui->treeView->setRootIsDecorated( false ); QModelIndex kTreeidx = m_pTreeModel->setRootPath( strDesktop ); 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.
- Double clicked the folder called "FM test", and it expands.
- Choose the folder called "test0", and the QListView shows all files without any folders in "test0".
- Double clicked the folder called "test0", and it expands.
- Choose the subfolder "testtest" of "test0".
- 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":http://dl.dropbox.com/u/24137109/test.zip
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 ^^:
@void MainWindow::OnTreeClicked( const QModelIndex& idx )
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.
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!!
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!
Hi, Marga, The solution works for me !!!
How about show us all the code u got ?!
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();
listModel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
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":http://doc.qt.nokia.com/latest/qfilesystemmodel.html#setFilter) 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.
The snippet about filter things are almost the same as mine, and it works for mine problem!!
Maybe the problem we got is different?!
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!!
Marga, could you solve this problem? I'm starting to think that is a Qt bug...
The given workarounds didn't work for me :(
I also have this problem on Qt 5.4.1. It seems this problem is still there: when I expand a subfolder suddenly this folder appears in the tableview that should just contain files.... very strange.
Moreover, sometimes the colums 1 and 2 appear even though I disabled colums with tableView->hideColumn(column 1 and 2). Very strange behavior that I have already spent too much time fixing :-). ReImplementing a FileSystemModel from scratch by subclassing QAbstractListModel would have been faster in my way. Right now I believe QFileSystemModel has some tricky parts that may contain bugs...