Results 1 to 3 of 3

Thread: QTreeView/QDirModel persisting current directory selection

  1. #1
    Join Date
    Apr 2008
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeView/QDirModel persisting current directory selection

    Hi, I am trying to work out how to persist the current selection in a QDirModel/QTreeView using QSettings so that I can expand the tree to that directory when the app restarts.

    I am using Qt 4.3.4.

    QTreeView does not appear to support saveState()/restoreState() so I guess I have to write a string URL for the currentItem() in the tree to QSettings and retrieve it later. In this case how do I convert from a string URL into a QModelItem in the View/Model?

    Is there a better way to use the built in mechanisms than the one I've outlined above? I already use QSettings to persist QApplication state and table header state but I can't see any built in mechanism for doing this on a tree.

    Thanks
    Dave

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView/QDirModel persisting current directory selection

    With QDirModel it is quite simple. Store the path in the settings and then upon initializing the application simply read it and expand its parents.

  3. #3
    Join Date
    Apr 2008
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView/QDirModel persisting current directory selection

    Just to close this off I found a reasonable solution.
    Calling setCurrentIndex() expands to the selected node.
    Calling setExpanded() expands the selected node too.
    Qt Code:
    1. class DirectoryTree : public QTreeView
    2. {
    3. public:
    4. void readSettings(QSettings& oSettings)
    5. {
    6. QString sLastSelection = oSettings.value("LastSelection").toString();
    7. if (!sLastSelection.isEmpty()) {
    8. QModelIndex index = m_oModel.index(sLastSelection);
    9. setCurrentIndex(index);
    10. setExpanded(index, true);
    11. }
    12. }
    13.  
    14. void writeSettings(QSettings& oSettings) {
    15. oSettings.setValue("LastSelection", m_oModel.filePath(currentIndex()));
    16. }
    17.  
    18. protected:
    19. QDirModel& m_oModel;
    20. };
    To copy to clipboard, switch view to plain text mode 

    cheers
    Dave
    Last edited by bigduv; 4th April 2008 at 13:29.

  4. The following user says thank you to bigduv for this useful post:

    babu198649 (31st July 2008)

Similar Threads

  1. Distributing QT application for Mac OS
    By mb0 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2007, 18:59
  2. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  3. Replies: 15
    Last Post: 21st April 2007, 17:46

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.