Results 1 to 5 of 5

Thread: QFileSystemModel::setFilter issue!!

  1. #1
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QFileSystemModel::setFilter issue!!

    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:
    Qt Code:
    1. setFilter( QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks );
    To copy to clipboard, switch view to plain text mode 

    The QTreeview is set with the QFilesystemModel which is setFilter like the code below:
    Qt Code:
    1. setFilter( QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks );
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QFileSystemModel::setFilter issue!!

    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.

  3. The following user says thank you to Rachol for this useful post:

    kennylemon (29th June 2011)

  4. #3
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileSystemModel::setFilter issue!!

    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:

    Qt 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. }
    6.  
    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();
    15.  
    16. QString strDesktop = QDesktopServices::storageLocation( QDesktopServices::DesktopLocation );
    17. ui->setupUi(this);
    18.  
    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 );
    24.  
    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 );
    30.  
    31. connect( ui->treeView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( OnTreeClicked( const QModelIndex& ) ) );
    32. }
    To copy to clipboard, switch view to plain text mode 

    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 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.

    btw, I am using Qt 4.7.

    Thanks in advanced.
    Kenny
    Last edited by kennylemon; 29th June 2011 at 06:01.

  5. #4
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QFileSystemModel::setFilter issue!!

    If you don't have to use QFileSystemModel and you can live with QDirModel, then following code should solve your problem:

    Qt Code:
    1. void MainWindow::OnTreeClicked( const QModelIndex& idx )
    2. {
    3. QFileInfo kFolderinfo = m_pTreeModel->filePath( idx );
    4.  
    5. m_pListModel->refresh(m_pListModel->index(kFolderinfo.filePath()));
    6. ui->listView->setRootIndex( m_pListModel->index(kFolderinfo.filePath() ) );
    7. }
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow),
    12. m_pListModel( NULL ),
    13. m_pTreeModel( NULL )
    14. {
    15. m_pListModel = new QDirModel();
    16. m_pTreeModel = new QDirModel();
    17.  
    18. QString strDesktop = QDesktopServices::storageLocation( QDesktopServices::DesktopLocation );
    19. ui->setupUi(this);
    20.  
    21. ui->listView->setModel( m_pListModel );
    22. m_pListModel->setFilter( QDir::NoDotAndDotDot | QDir::Files );
    23. ui->listView->setRootIndex( m_pListModel->index(strDesktop) );
    24.  
    25. ui->treeView->setModel( m_pTreeModel );
    26. m_pTreeModel->setFilter( QDir::NoDotAndDotDot | QDir::AllDirs );
    27. ui->treeView->setRootIsDecorated( false );
    28. ui->treeView->setRootIndex( m_pTreeModel->index(strDesktop) );
    29.  
    30. connect( ui->treeView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( OnTreeClicked( const QModelIndex& ) ) );
    31. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileSystemModel::setFilter issue!!

    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.
    Last edited by kennylemon; 29th June 2011 at 11:55.

Similar Threads

  1. Using bindValue with QSqlTableModel.setFilter()
    By manuels in forum Qt Programming
    Replies: 0
    Last Post: 11th December 2010, 14:16
  2. QSqlTablemodel->setFilter()
    By codeman in forum Qt Programming
    Replies: 4
    Last Post: 9th June 2009, 16:52
  3. trouble with QSqlRelationalTableModel and setFilter
    By yleesun in forum Qt Programming
    Replies: 5
    Last Post: 3rd March 2009, 08:02
  4. QDataTable setFilter
    By cristiano in forum Qt Programming
    Replies: 10
    Last Post: 17th November 2006, 02:38
  5. Replies: 6
    Last Post: 4th April 2006, 08:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.