Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] A way to save QTreeWidget to file
Forum Update on Tuesday, May 27th 2025

[Solved] A way to save QTreeWidget to file

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 7.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chamoda
    wrote on last edited by
    #1

    Hello,

    I'm trying to save QTreeWidget to file and load it another time. I wrote this code but it didnt work.

    Save..

    @ QList<QTreeWidgetItem *> myList;
    for(int i = 0; i < treeWidget->topLevelItemCount(); i++)
    {
    myList << treeWidget->takeTopLevelItem(i);
    }

        QDataStream outStream(&file);
        outStream.setVersion(QDataStream::Qt_4_7);
        outStream << myList;@
    

    Load..

    @QDataStream inStream(&file);
    inStream.setVersion(QDataStream::Qt_4_7);
    inStream >> myList; // error
    treeWidget->addTopLevelItems(myList);@

    Is there any simple function or way to do this?

    thanks

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      "This":http://doc.qt.digia.com/qt/datastreamformat.html page contains a list of all the Qt types/objects that are serializable, QTreeWidgetItem isn't one of them I'm afraid. AFAIK there is no built in way to directly stream a QTreeWidget to a file. I think you'll have to traverse the data structure and save it in a suitably structured file format (JSON or XML for example).

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • C Offline
        C Offline
        chamoda
        wrote on last edited by
        #3

        thanks for your reply jazzycamel. Any example code to do it with XML?

        1 Reply Last reply
        0
        • TheBadgerT Offline
          TheBadgerT Offline
          TheBadger
          wrote on last edited by
          #4

          I use a Qt Library called "Qtilities":http://www.qtilities.org/. It has its own way of "building trees":http://www.qtilities.org/docs/master/page_tree_structures.html with Observers

          I has its own "observer widget":http://www.qtilities.org/docs/master/page_observer_widgets.html that can then be used to display this tree.

          Using the built in functions, this tree can then saved to an XML or binary file. Perhaps you can also use this library as is or look at the code of how it is done.

          There is a section in the docs on "saving and loading the tree to XML":http://www.qtilities.org/docs/master/page_tree_structures.html#tree_xml that will do all the work for you.


          Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chamoda
            wrote on last edited by
            #5

            Thanks Badger. I didn't know about Qtilities before.

            also I found a way to do it with QStringList because my structure was simple..

            Like this

            @
            QList<QStringList> myList;

                    QDataStream inStream(&file);
                    inStream.setVersion(QDataStream::Qt_4_7);
                    inStream >> myList;
                     for(int i = 0; i < myList.size(); i++)
                    {
                     QTreeWidgetItem *item = new QTreeWidgetItem(myList.at(i));
                     treeWidget->addTopLevelItem(item);
                    }
            

            @

            Thanks all for replying. Problem solved!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved