Results 1 to 4 of 4

Thread: Selecting file in QFileSystemModel tree view

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4

    Default Selecting file in QFileSystemModel tree view

    Hello,

    I have a tree view displaying a QFileSystemModel, and I need to highlight a specific file in the tree. But since QFileSystemModel is loading the children asynchronously, using selectionModel()->setCurrentIndex() seems to be failing, unless the path has been previously expanded in the tree by the user.

    Can anyone recommend a way of selecting a file in QFileSystemModel tree view? The only way I can think of is to manually parse the path, call expand() on the tree nodes, wait until QFileSystemModel fetches the directory (how?) and after the entire path is expanded, call selectionModel()->setCurrentIndex(). Isn't there an easier way?

    Thanks!

  2. #2
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4

    Default Re: Selecting file in QFileSystemModel tree view

    So I came up with something like this. I'm not too excited about the code, but I couldn't think of a better way.

    Qt Code:
    1. // assumes that path and rootPath use the same dir separator and the same letter case
    2. void PopulatePathInModel(const QString& path, const QString& rootPath, QFileSystemModel* model)
    3. {
    4. QStringList parts = path.split(QDir::separator());
    5.  
    6. QString currPath;
    7. foreach (QString part, parts)
    8. {
    9. if (!currPath.isEmpty()) currPath += QDir::separator();
    10. currPath += part;
    11.  
    12. // no need to populate dirs outside of our area of interest
    13. if (!currPath.startsWith(rootPath)) continue;
    14.  
    15. if (QFileInfo(currPath).isDir())
    16. {
    17. QModelIndex idx = model->index(currPath);
    18. if (idx.isValid())
    19. {
    20. while (model->canFetchMore(idx))
    21. {
    22. model->fetchMore(idx);
    23. }
    24. }
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Selecting file in QFileSystemModel tree view

    I know the thread is a bit dated, but I stumbled across this at http://stackoverflow.com/questions/5...mmodel-by-path

    Much simpler, methinks.

    Qt Code:
    1. tree->setCurrentIndex(fsModel->index(QDir::currentPath())); // or any path
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Selecting file in QFileSystemModel tree view

    Did you found a better solution yet? I'm having the same problem and doing this do not work:

    Qt Code:
    1. tree->setCurrentIndex(fsModel->index(myPath));
    To copy to clipboard, switch view to plain text mode 


    Quote Originally Posted by Cougar View Post
    So I came up with something like this. I'm not too excited about the code, but I couldn't think of a better way.

    Qt Code:
    1. // assumes that path and rootPath use the same dir separator and the same letter case
    2. void PopulatePathInModel(const QString& path, const QString& rootPath, QFileSystemModel* model)
    3. {
    4. QStringList parts = path.split(QDir::separator());
    5.  
    6. QString currPath;
    7. foreach (QString part, parts)
    8. {
    9. if (!currPath.isEmpty()) currPath += QDir::separator();
    10. currPath += part;
    11.  
    12. // no need to populate dirs outside of our area of interest
    13. if (!currPath.startsWith(rootPath)) continue;
    14.  
    15. if (QFileInfo(currPath).isDir())
    16. {
    17. QModelIndex idx = model->index(currPath);
    18. if (idx.isValid())
    19. {
    20. while (model->canFetchMore(idx))
    21. {
    22. model->fetchMore(idx);
    23. }
    24. }
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    Added after 19 minutes:


    I managed to do it in a more cleaner way but still I'm wondering if there is not a better way to do it:

    Qt Code:
    1. while (myFileSystemModel->canFetchMore(itemIndex)) {
    2. myFileSystemModel->fetchMore(itemIndex);
    3. itemIndex = itemIndex.parent();
    4. }
    To copy to clipboard, switch view to plain text mode 

    We do not have a cleaner way to sync for a QFileSystemModel index?
    Last edited by christophe; 28th August 2011 at 01:52.

Similar Threads

  1. Replies: 8
    Last Post: 6th May 2010, 11:17
  2. Qtreeview : To get File name and path from Tree View
    By augusbas in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2010, 13:14
  3. QFileSystemModel file icon overlays
    By squidge in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2009, 11:06
  4. Tree View
    By bismitapadhy in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2009, 06:31
  5. tree view!!
    By Seema Rao in forum Qt Programming
    Replies: 4
    Last Post: 9th May 2006, 14:09

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.