Results 1 to 9 of 9

Thread: Find Files QFileSystemModel

  1. #1
    Join Date
    Dec 2014
    Posts
    49
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Windows

    Default Find Files QFileSystemModel

    I am displaying a QFileSystemModel in a QTreeView to display the directories, and then i have a QListView that displays the files of the selected directory.

    I want to implement search functionality, so that if i am currently on the root directory, it will search the children directories, and display all matching files in the QListView.

    I have tried, and currently still playing with QSortFilterModelProxy, but when it searches, it searches from the currently selected folder, back up to the ancestor list, instead of going into the children.

    Any help or guidance would be much appreciated

  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: Find Files QFileSystemModel

    How do you populate the list view during non-search operation?

    Do you have another proxy model between the file system model and are filtering on parent?
    Or is that the same proxy model instance and you are changing the filter criteria when searching?
    Or do you have a list model that is populated from a directory listing?

    Cheers,
    _

  3. #3
    Join Date
    Dec 2014
    Posts
    49
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Find Files QFileSystemModel

    Currently i have the following

    QTreeView powered by DirectoryStructureModel (extended QFileSystemModel) and DirectoryStructureProxyModel (extended QSortFilterProxyModel)
    QListView powered by FileListModel (extended QFileSystemModel) and FileListModelProxy (extended QSortFilterProxyModel).

    When the QTreeView selection changes, the QListView updates the root path and the QListView displays the contents of the directory, based on the filter flags in FileListModelProxy.


    So what i want to have is a line edit, that on textChanged signal, QListView displays all matches, from Root to full extents.


    Ex.

    Given the structure below, and Assets is currently selected, the list view is currently displaying Scenes and Textures folders. Entering "L" into the search edit, would clear the list view, and display only "Level_01.xml"

    -Assets
    --Scenes
    ---Level_01.xml
    --Textures
    ---Player.png

  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: Find Files QFileSystemModel

    In your setup you will need to

    - set the root path of the FileListModel to the very base root path
    - in FileListModelProxy accept all rows that either match or which are directories and contain a match somewhere in their sub tree

    Alternatively you could just have a special purpose list model for the second view.
    That model would get a root path and the search string.
    - If the search string is empty, it just lists the root path.
    - If it is not empty, it uses a recursive QDirIterator starting at the top root path

    More or less it could always use a QDirIterator, the presence of the search string would change the path to start with and the recursiveness.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2014
    Posts
    49
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Find Files QFileSystemModel

    I have updated filterAcceptsRow for FileListViewModelProxy, and now when it hits a directory, it recursively dives in, and matches on filterText_, however, the display results are not what is expected.

    Qt Code:
    1. bool FileListViewModelProxy::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const {
    2. QFileSystemModel* fileSystemModel = qobject_cast<QFileSystemModel*>(sourceModel());
    3.  
    4. if (sourceParent == fileSystemModel->index(fileSystemModel->rootPath())) {
    5. return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
    6. }
    7.  
    8. QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
    9. QFileInfo fileInfo = fileSystemModel->fileInfo(index);
    10. QString fileName = fileInfo.fileName();
    11.  
    12. if (fileName.startsWith(filterText_))
    13. return true;
    14.  
    15. if (fileInfo.isDir())
    16. return searchDirectory(fileInfo);
    17.  
    18. return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool FileListViewModelProxy::searchDirectory(QFileInfo fileInfo) const {
    2. QString filePath = fileInfo.filePath();
    3. QDir dir(fileInfo.filePath());
    4. QString dirName = fileInfo.fileName();
    5.  
    6. QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
    7. for (int i = 0; i < fileList.size(); i++) {
    8. QFileInfo fileInfo = fileList[i];
    9. QString fileName = fileInfo.fileName();
    10.  
    11. if (fileName.startsWith(filterText_))
    12. return true;
    13.  
    14. if (fileInfo.isDir()) {
    15. return searchDirectory(fileInfo);
    16. }
    17.  
    18. }
    19.  
    20. return false;
    21. }
    To copy to clipboard, switch view to plain text mode 

  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: Find Files QFileSystemModel

    What do you get and what do you expect to see?

    Cheers,
    _

  7. #7
    Join Date
    Dec 2014
    Posts
    49
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Find Files QFileSystemModel

    I get Scenes and Textures folder displaying in the ListView, however, i expect/want Level_01.xml to be only displayed in the ListView.

    I'm thinking i need to go the seperate model for search results and populate the list view with it when search is enabled.

  8. #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: Find Files QFileSystemModel

    Hmm, does it work if your filter does not except the directories?

    But yes, I would have always gone for a special purpose model, as suggested in #4
    Cheers,
    _

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

    NIteLordz (8th September 2016)

  10. #9
    Join Date
    Dec 2014
    Posts
    49
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Find Files QFileSystemModel

    I wound up implementing the custom model, and it works as i expected.

    Thanks for the help!

Similar Threads

  1. Failing to SetFilter of QFileSystemModel for .mp3/.avi files
    By owais_blore in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2012, 07:44
  2. Rename a lot of files by QFile and QFileSystemModel
    By stereoMatching in forum Newbie
    Replies: 1
    Last Post: 25th July 2012, 19:55
  3. adding files to QFileSystemModel
    By gluchu in forum Newbie
    Replies: 1
    Last Post: 11th July 2012, 01:25
  4. how can i list of files to QFileSystemModel?
    By aurora in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2012, 08:28
  5. Select all files [QTableView et QFileSystemModel]
    By snooker9 in forum Qt Programming
    Replies: 0
    Last Post: 23rd December 2011, 09:17

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.