Results 1 to 2 of 2

Thread: QFileSystemModel with QTreeView Questions

  1. #1

    Default QFileSystemModel with QTreeView Questions

    I'm new to Qt and I'm trying a to create file system tree showing folders only, but I'm failing miserably. I've got three questions I was hoping for some help with:

    1) How do you check if a directory has been loaded by the model? There's the directoryLoaded() signal, which tells you when a directory has finished loading, but I can't find a function to check at a random point if a directory has been loaded. One option would be to check if the rowCount() is 0, but a rowCount() of 0 could also mean the directory has no subfolders so you wouldn't be sure if it was loaded or not. The only other option is to save a list of loaded directories, but that would be very inefficient. Is there a better option?

    2) How do you get the list of children of the root node? For other nodes I use:
    Qt Code:
    1. QModelIndexList children;
    2. int numChildRows = fileSystem->rowCount(modelIndex);
    3. for (int i = 0 ; i < numChildRows ; ++i)
    4. fileSystem->fetchMore(modelIndex.child(i, 0));
    To copy to clipboard, switch view to plain text mode 
    This won't work for the root note because the root modelIndex is invalid, so I'm not sure how to iterate through the root's children.

    3) The third problem is that the QTreeView shows expansion marks next to folders that have no subfolders and I only want it to have expansion marks next to folders that can be expanded. One solution is to subclass QFileSystemModel and override hasChildren() so that it only returns true if the folder has subfolders. This works, but makes it incredibly slow to expand a branch with a lot of folders in (as much as 15 seconds) so that's not viable. Another solution I've seen suggested is to call fetchMore() more on all subfolders in the expanded directory, but when I tried this the QTreeView doesn't update to remove the expansion marks. Here's my code:

    Constructor:
    Qt Code:
    1. fileSystem = new QFileSystemModel(parent);
    2. fileSystem->setReadOnly(false);
    3. fileSystem->setRootPath(m_pqfsmFolderSystem->myComputer().toString());
    4. fileSystem->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
    5.  
    6. directoryTree = new QTreeView(parent);
    7. directoryTree->setModel(fileSystem);
    8. directoryTree->setEditTriggers(QTreeView::NoEditTriggers);
    9. directoryTree->hideColumn(1);
    10. directoryTree->hideColumn(2);
    11. directoryTree->hideColumn(3);
    12. directoryTree->header()->hide();
    13.  
    14. connect(directoryTree, SIGNAL(expanded(const QModelIndex &)), this, SLOT(nodeExpanded(const QModelIndex &)));
    15. connect(fileSystem, SIGNAL(directoryLoaded(const QString &)), this, SLOT(directoryLoaded(const QString &)));
    To copy to clipboard, switch view to plain text mode 

    Relevant functions
    Qt Code:
    1. void FileTree::nodeExpanded(const QModelIndex & modelIndex)
    2. {
    3. // Need to find out how to check if directory is already loaded
    4. //if (directory loaded)
    5. // fetchChildren(modelIndex);
    6. //else
    7. directoriesToFetch.push_back(krqmiModelIndex);
    8. }
    9.  
    10. void FileTree::directoryLoaded(const QString & path)
    11. {
    12. if (directoriesToFetch.empty())
    13. return;
    14.  
    15. QVector<QModelIndex>::iterator directoryIndex;
    16. for (directoryIndex = directoriesToFetch.begin() ; directoryIndex != directoriesToFetch.end() ; ++directoryIndex)
    17. {
    18. if (path == fileSystem->filePath(*directoryIndex))
    19. {
    20. fetchChildren(*directoryIndex);
    21. directoriesToFetch.erase(directoryIndex);
    22. break;
    23. }
    24. }
    25. }
    26.  
    27. void FileTree::fetchChildren(const QModelIndex & modelIndex)
    28. {
    29. int numChildren = fileSystem->rowCount(modelIndex);
    30. for (int i = 0 ; i < numChildren ; ++i)
    31. fileSystem->fetchMore(krqmiModelIndex.child(i, 0));
    32. }
    To copy to clipboard, switch view to plain text mode 

    I've debugged through and fetchMore() is being called for all the sub-folders, but the QTreeView isn't being updated to remove the expansion symbols for directories with no subfolders. Is there something else I need to do?

    Sorry for all the questions. I can't seem to get anything to work. I think I'm just stupid

  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: QFileSystemModel with QTreeView Questions

    Quote Originally Posted by MJ9797 View Post
    2) How do you get the list of children of the root node? For other nodes I use:
    To get indexes of children of any node you just pass the index of that node as the third argument to the model's index() method
    Qt Code:
    1. const int numChildren = model->rowCount(index);
    2. for (int i = 0; i < numChildren; ++i) {
    3. const QModelIndex child = model->index(i, 0, index);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Filter QListView/QTreeView using QFileSystemModel
    By Alundra in forum Qt Programming
    Replies: 1
    Last Post: 17th September 2015, 15:55
  2. Replies: 0
    Last Post: 5th April 2011, 12:40
  3. Replies: 1
    Last Post: 21st February 2011, 17:35
  4. Replies: 3
    Last Post: 17th August 2010, 17:58
  5. Replies: 0
    Last Post: 20th July 2010, 14:55

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.