Results 1 to 5 of 5

Thread: Help with QFileSystemModel

  1. #1
    Join Date
    Oct 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Help with QFileSystemModel

    How can QFileSystemModel be used to create a QStringList of full pathnames of directories?

    This is what I'm trying to do:

    Qt Code:
    1. QStringList Info::getDirectoryTree(const QString & path)
    2. {
    3. QStringList paths;
    4.  
    5. QModelIndex parent = fileSystemModel->index(path);
    6. int rowCount = fileSystemModel->rowCount(parent);
    7. int columnCount = fileSystemModel->columnCount(parent);
    8. for (int i=0; i<rowCount; i++)
    9. {
    10. for (int j=0; j<columnCount; j++)
    11. {
    12. QModelIndex mi = fileSystemModel->index(i, j, parent);
    13. QFileInfo fileInfo = fileSystemModel->fileInfo(mi);
    14. if (!paths.contains(fileInfo.absoluteFilePath()))
    15. {
    16. paths << fileInfo.absoluteFilePath();
    17. }
    18. bool hasChildren = fileSystemModel->hasChildren(mi);
    19. if (hasChildren)
    20. {
    21. paths << getDirectoryTree(fileInfo.absoluteFilePath());
    22. }
    23. }
    24. }
    25.  
    26. return paths;
    27. }
    To copy to clipboard, switch view to plain text mode 

    If you set the root path on fileSystemModel, then call getDirectoryTree() above, it works fine for the first directory level. However, when it recursively calls getDirectoryTree() using the child nodes, the rowCount is always 0.

    Obviously I'm just not understanding the concept behind QAbstractItemModel in order to iterate through all values. In my case there is a root directory with about 100 subdirectories, most of which have several subdirectories, etc. (over 3,500 total).

    All I want to do is get all the paths of the directories in a QFileSystemModel.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with QFileSystemModel

    QFileSystemModel can return zero for rowCount until the node is populated, as it uses a seperate thread for populating the tree. It'll send a signal when the count has changed.

    I'm not really sure how to get it to what you want, and it's not really meant for that.

    Maybe QDir would be better for you, as that isn't threaded, and you can call entryInfoList to get a list of all files and directories in the directory.

  3. #3
    Join Date
    Oct 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with QFileSystemModel

    Maybe that's the problem. I've already implemented the feature a few different ways, using QDirModel, QDir, and QDirIterator. The issue is with performance. There are almost 4,000 directories and it takes 3-4 seconds to perform the operation. I was just hoping for a faster solution.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with QFileSystemModel

    What is the intended last result? As in, what are you going to do with your list of filenames once you have them?

    There might be better way.

  5. #5
    Join Date
    Oct 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with QFileSystemModel

    The intended use is to find all directories below a starting directory that contain a certain string. That list will be presented at various places within the application and the search string may be different.

Similar Threads

  1. Notifying QFileSystemModel about changes
    By squidge in forum Qt Programming
    Replies: 3
    Last Post: 23rd November 2009, 23:24
  2. QFileSystemModel file icon overlays
    By squidge in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2009, 11:06
  3. Known problem with filters and QFileSystemModel?
    By Kumosan in forum Qt Programming
    Replies: 3
    Last Post: 27th August 2009, 08:14
  4. QDirModel or QFileSystemModel?
    By ricardo in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2009, 17:10
  5. QFileSystemModel has no sorting
    By mpi in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2009, 08:14

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.