Results 1 to 8 of 8

Thread: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterProxymo

  1. #1
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Get names of filtered files,folders in list using QFileSystemModel,QSortFilterProxymo

    I am using QFileSystemModel for exploring my drive and QSortFilterProxyModel for filtering all the files and folders in it. It works well when i try to display the model in a QTreeview. Now my problem is, when i try to get all the files and folders names in a list. It gives me unfiltered names. I am using this:


    Qt Code:
    1. QFileSystemModel *fileSystemModel;
    2. fileSystemModel = new QFileSystemModel(this);
    3. QSortFilterProxyModel proxyModel = new QSortFilterProxyModel(targetDir);
    4. QModelIndex rootModelIndex = fileSystemModel->setRootPath(path);
    5. QStringList filters;
    6. filters <<"*.jpeg" <<"*.pict" ;
    7. fileSystemModel->setNameFilters(filters);
    8. fileSystemModel->setNameFilterDisables(0);
    9. proxyModel->setSourceModel(fileSystemModel);
    10. fileSystemModel->fetchMore(rootModelIndex);
    11. fileSystemModel->sort(0,Qt::AscendingOrder);
    12. ui->treeView_2->setModel(proxyModel);
    13. ui->treeView_2->setRootIndex(proxyModel->mapFromSource(rootModelIndex));
    14. connect(fileSystemModel,SIGNAL(directoryLoaded(QString)),this,SLOT(onDirLoaded(QString)));
    15.  
    16. for names list i am using the Directoryloaded() signal of QFileSystemModel:
    17.  
    18. void MainWindow::onDirLoaded(QString path)
    19. {
    20. QModelIndex index = fileSystemModel->index(path);
    21. int rowCount = fileSystemModel->rowCount(index);
    22. for(int i=0;i<rowCount;i++)
    23. {
    24. QModelIndex mi = fileSystemModel->index(i,0,index);
    25.  
    26. QFileInfo fileInfo = fileSystemModel->fileInfo(mi);
    27. if (!templist.contains(fileInfo.absoluteFilePath()))
    28. {
    29. templist << fileInfo.absoluteFilePath();
    30. tempNamelist << fileInfo.fileName();
    31. }
    32. if(!bloaded)
    33. {
    34. if(fileSystemModel->hasChildren(mi))
    35. {
    36. QModelIndex index1 = fileSystemModel->index(i,0,index);
    37. int rowCount1 = fileSystemModel->rowCount(index1);
    38. if(rowCount1)
    39. {
    40. onDirLoaded(fileInfo.absoluteFilePath());
    41. }
    42. }
    43. }
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

    After this i am getting list of all the files and folders but its unfiltered names.I want filtered names. Is there anyone who can help me in this ? Your help will really appreciate guys.
    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro

    Not sure what your problem is.
    The slot works with the QFIleSystemModel instance which of course doesn't know anything about things the QSortFilterProxyModel does.

    If you want to get the things the view sees, you have to get the data from the same model that the view uses.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro

    Thanks for your response, I am doing the same as you are saying. I am trying to get the names from the same model but i think i am missing something. Can you tell me how to use the data of that model ? FYI, my problem is I want filtered names list of only those files and folders which have a particular type of file extension(*.mp3,*.jpg).
    Really appreciate if you could help me out of this.
    Thanks.
    Last edited by Dev; 29th October 2013 at 04:29.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro

    Quote Originally Posted by Dev View Post
    Thanks for your response, I am doing the same as you are saying. I am trying to get the names from the same model but i think i am missing something.
    The code you posted used different models. The view uses a filter proxy model, the slot uses the file system model.

    If your current code is different and you use the filtered data in both places and it still does not work, please post updated code.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro

    thanks,
    Its my updated code.First half of the code is working fine.

    Qt Code:
    1. QFileSystemModel *fileSystemModel;
    2. fileSystemModel = new QFileSystemModel(this);
    3. QSortFilterProxyModel proxyModel = new QSortFilterProxyModel(targetDir);
    4. QModelIndex rootModelIndex = fileSystemModel->setRootPath(path);
    5. QStringList filters;
    6. filters <<"*.jpeg" <<"*.pict" ;
    7. fileSystemModel->setNameFilters(filters);
    8. fileSystemModel->setNameFilterDisables(0);
    9. proxyModel->setSourceModel(fileSystemModel);
    10. fileSystemModel->fetchMore(rootModelIndex);
    11. fileSystemModel->sort(0,Qt::AscendingOrder);
    12. ui->treeView_2->setModel(proxyModel);
    13. ui->treeView_2->setRootIndex(proxyModel->mapFromSource(rootModelIndex));
    To copy to clipboard, switch view to plain text mode 

    after this i am trying to fill the names of files and folders in the model. so i am calling a signal directoryloaded() for files and folders names from the model and the model name is fileSystemModel. Thats i am using that model in other part of my code. Am i on right way ? If not then tell me what is the proper solution for getting names. Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro

    Ah, but the problem is in the other part of the code.

    See, for the view you are using
    Qt Code:
    1. ui->treeView_2->setModel(proxyModel);
    To copy to clipboard, switch view to plain text mode 

    But for the slot you are using
    Qt Code:
    1. int rowCount = fileSystemModel->rowCount(index);
    To copy to clipboard, switch view to plain text mode 

    Are those the same? I.e. is that the same pointer just with different names in different code parts?

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    Dev (15th November 2013)

  8. #7
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro

    thanks, i have done it. it works fine.
    But In my second part of the code, i am not getting the filtered names of the files and subfolders as my treeview is displaying.
    Can you please tell what is problem behind this ?

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro

    Just to be sure: your onDirectoryLoaded() now also uses the proxyModel instance?

    Cheers,
    _

Similar Threads

  1. how can i list of files to QFileSystemModel?
    By aurora in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2012, 08:28
  2. Find List of Special Folders in Windows
    By Dilshad in forum Qt Programming
    Replies: 6
    Last Post: 13th February 2012, 05:13
  3. How to get a list of file names in a directory?
    By khanhsk in forum Qt Programming
    Replies: 2
    Last Post: 8th February 2012, 03:34
  4. QFileSystemModel hiding folders
    By been_1990 in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2011, 18:03
  5. Replies: 4
    Last Post: 20th September 2007, 14:11

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.