Results 1 to 6 of 6

Thread: Any way to save the collapsed/expanded states for all items in a QTreeWidget ?

  1. #1
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Any way to save the collapsed/expanded states for all items in a QTreeWidget ?

    Happy new year !


    is there any simple way to save to a Qsettings the collapsed/expanded state of all the items in a QTreeWidget ? Smething like savegeometry . Just asking, would be nice.

  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: Any way to save the collapsed/expanded states for all items in a QTreeWidget ?

    No, you have to iterate over items and store a list of expanded nodes yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Any way to save the collapsed/expanded states for all items in a QTreeWidget ?

    Start at the root of the tree, and recursively work your way down, calling each QTreeWidgetItem's isExpanded() method. If you make a bitstring, you can turn on the bit for each item that is expanded and turn it off for each one that is collapsed. Store this in QSettings. When you retrieve it, work you way back through it again, calling setExpanded() with true for the 1 bits, false for the zero bits.

    But this only works if your tree never changes between the time you save it and the time it is loaded again. I don't know what your tree represents, but if the tree contents or structure can be changed from outside your program (like a file system, for example), then saving and restoring the state is meaningless.

  4. #4
    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: Any way to save the collapsed/expanded states for all items in a QTreeWidget ?

    Quote Originally Posted by d_stranz View Post
    I don't know what your tree represents, but if the tree contents or structure can be changed from outside your program (like a file system, for example), then saving and restoring the state is meaningless.
    Not if you also store the id of the node (like a file path in case of a filesystem tree). Then you match items by the id and not by the order of items in the tree.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Any way to save the collapsed/expanded states for all items in a QTreeWidget ?

    Yes, that would work for trees with externally mutable content. So, instead of storing a bit for every element in the tree, you store only the full paths to the first collapsed item in each branch. By definition, everything above that node must be expanded, and everything below is collapsed. Non-matching items are ignored.

  6. #6
    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: Any way to save the collapsed/expanded states for all items in a QTreeWidget ?

    Of course there is one more downside. If you have two nodes with the same id during two different runs of the program, it doesn't mean they are the same node, it might be they just have the same id (e.g. you can have a directory called "x", then delete it and then create another directory called "x" with different content and the question is whether it should be expanded or not).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 22nd July 2010, 04:40
  2. How to save QGraphicsScene Items to a file?
    By yagabey in forum Qt Programming
    Replies: 5
    Last Post: 28th February 2010, 11:16
  3. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 06:03
  4. Save & Restore a selection in QTreeWidget
    By youkai in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2008, 20:54
  5. QGraphicsItem problem - how to save items info ??
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2006, 13:17

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.