Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Save and restore Qtreewidget
Qt 6.11 is out! See what's new in the release blog

Save and restore Qtreewidget

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 2.0k 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.
  • M Offline
    M Offline
    MarcinOS
    wrote on last edited by MarcinOS
    #1

    HI,
    How can I do this?
    The idea is to save the entire content of Qtreewidget to a file and load it. Preferably in some native QT format to do it quickly and safely.
    I can do it from Python itself, but maybe there are some simpler mechanisms built into PyQT?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      There's no premade model serialization so you have to implement it yourself.

      You could use the QDataStream class for that but depending on the content of your QTreeWidget, generating a JSON data structure might be enough. You could store the resulting string using QSettings along other parameters of your application.

      But first things first, what do you want to save and why ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        There's no premade model serialization so you have to implement it yourself.

        You could use the QDataStream class for that but depending on the content of your QTreeWidget, generating a JSON data structure might be enough. You could store the resulting string using QSettings along other parameters of your application.

        But first things first, what do you want to save and why ?

        M Offline
        M Offline
        MarcinOS
        wrote on last edited by MarcinOS
        #3

        @SGaist My program scans the contents of the database of the graphics engine used in television. These databases are quite large, so the entire procedure of scanning them and arranging them in the right way in Qtreewidget takes a lot of time. However, these databases rarely change. I would like to be able to save the contents of Qtreewidget after scanning so that I can open it and work with it without having to scan the engine every time.

        JonBJ SGaistS 2 Replies Last reply
        0
        • M MarcinOS

          @SGaist My program scans the contents of the database of the graphics engine used in television. These databases are quite large, so the entire procedure of scanning them and arranging them in the right way in Qtreewidget takes a lot of time. However, these databases rarely change. I would like to be able to save the contents of Qtreewidget after scanning so that I can open it and work with it without having to scan the engine every time.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @MarcinOS
          A QTreeWidget is a QTreeView with an internal model attached to it. You cannot serialize a QTreeView because it is a QWidget/QObject, and those cannot be serialized. In principle you should just be able to serialize the model part, and use that to reconstruct it in a new QTreeWidget when reading back in.

          If QTreeWidget saves all its customizable data in the model via roles you can use model.data() to read and save the data in all the roles and model.setData() to restore it. Or, you can do that for the individual QTreeWidgetItems, I notice that they have QDataStream & operator<<(QDataStream &out, const QTreeWidgetItem &item) (have to check how you do this from Python/PySide, might need to call https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QTreeWidgetItem.html#PySide6.QtWidgets.PySide6.QtWidgets.QTreeWidgetItem.write directly) already written for serializing them to/from a QDataStream. You may also need to save/restore the PySide6.QtWidgets.QTreeWidget.headerItem().

          1 Reply Last reply
          0
          • M MarcinOS

            @SGaist My program scans the contents of the database of the graphics engine used in television. These databases are quite large, so the entire procedure of scanning them and arranging them in the right way in Qtreewidget takes a lot of time. However, these databases rarely change. I would like to be able to save the contents of Qtreewidget after scanning so that I can open it and work with it without having to scan the engine every time.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @MarcinOS in that case, I would rather recommend that you get the database, apply the processing you need to have it in a standard known form and then use that form with your application. That way you stay independent of whatever widget you will use to show the data.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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