PDA

View Full Version : QFileSystemModel::setFilter issue!!



kennylemon
28th June 2011, 12:36
Hi all:

I am trying to construct a custom file manager, using QListview, QTreeview, and two QFileSystemModel!!

The QListview is set with the QFileSystemModel( Listmodel ) which is setFilter like the code below:

setFilter( QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks );

The QTreeview is set with the QFilesystemModel which is setFilter like the code below:

setFilter( QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks );

When the file manager shows up at the beginning, everything looks perfect as the first attach file.

But after few steps, the filter in the Listmodel seems to become invalid as the second attach file.
1. Double click a folder called "test", which includes a subfolder, in the treeview. And the subfolder icon shows up.
2. Choose a subfolder of the folder "test", then the listview shows the files in the subfolder
3. Choose back to the folder "test", and the listview shows all files in the folder including the subfolder.

The result is not correct with the filter being set to the Listmodel!!!

Could anyone tell me what's wrong?!?!?
Thanks in advanced.

Kenny

Rachol
28th June 2011, 12:55
I am afraid that there is some filter resetting in your code that creates the mess. Provide smallest possible working code that reproduces the problem.

kennylemon
29th June 2011, 05:52
Thank you for your reply.

I've tried to make another simple test project.
And the code in MainWindow.cpp is pretty simple as below:



void MainWindow::OnTreeClicked( const QModelIndex& idx )
{
QFileInfo kFolderinfo = m_pTreeModel->filePath( idx );
ui->listView->setRootIndex( m_pListModel->setRootPath( kFolderinfo.filePath() ) );
}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
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& ) ) );
}


This simple project also make the same issue.
After the test project, I've do some search.
And I have found out the doc Qt 4.4 QFileSystemModel (http://web.mit.edu/qt-dynamic/www/qfilesystemmodel.html) tells that the function, setRootPath, will reset the model while the path is changed!!!
I cannot figure out the word "reset", and whether it is related to weird issur or not.

And the related description is not shown in Qt 4.7 QFileSystemModel (http://doc.qt.nokia.com/4.7-snapshot/qfilesystemmodel.html#setRootPath).

btw, I am using Qt 4.7.

Thanks in advanced.
Kenny

Rachol
29th June 2011, 10:26
If you don't have to use QFileSystemModel and you can live with QDirModel, then following code should solve your problem:


void MainWindow::OnTreeClicked( const QModelIndex& idx )
{
QFileInfo kFolderinfo = m_pTreeModel->filePath( idx );

m_pListModel->refresh(m_pListModel->index(kFolderinfo.filePath()));
ui->listView->setRootIndex( m_pListModel->index(kFolderinfo.filePath() ) );
}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_pListModel( NULL ),
m_pTreeModel( NULL )
{
m_pListModel = new QDirModel();
m_pTreeModel = new QDirModel();

QString strDesktop = QDesktopServices::storageLocation( QDesktopServices::DesktopLocation );
ui->setupUi(this);

ui->listView->setModel( m_pListModel );
m_pListModel->setFilter( QDir::NoDotAndDotDot | QDir::Files );
ui->listView->setRootIndex( m_pListModel->index(strDesktop) );

ui->treeView->setModel( m_pTreeModel );
m_pTreeModel->setFilter( QDir::NoDotAndDotDot | QDir::AllDirs );
ui->treeView->setRootIsDecorated( false );
ui->treeView->setRootIndex( m_pTreeModel->index(strDesktop) );

connect( ui->treeView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( OnTreeClicked( const QModelIndex& ) ) );
}

kennylemon
29th June 2011, 11:37
Thanks for your reply!!
And I've tried to use QDirModel, it really work!!

But I really have to use QFileSystemMode because of some reasons:
1. The file manager needs the functions applied by the watcher.
2. The performance difference between QDirModel and QFileSystemModel is what I concern, so I use QFileSystemModel.

any idea??
The test project and files could be download here (http://dl.dropbox.com/u/24137109/test.zip).