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. How to save/pickle Qt object's state
Forum Updated to NodeBB v4.3 + New Features

How to save/pickle Qt object's state

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 4 Posters 2.5k 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    Hi,
    I actually didn't find much about that online, so here is the question.

    I would like to save the exact state on an object that is a reimplementation of a Qt class (in my case QGraphicsView, QGraphicsItems, etc.). I tried to pickle it, but all I get is an error

    TypeError: can't pickle PySide2.(...) objects
    

    I assume this has to do with the fact that these qt classes are implemented in c++. But I wouldn't assume it to be impossible since all the standard an object's current state describing methods like __dict__ etc. work, but I don't have a lot of experience in doing stuff like that.
    Is there a convenient way to save a qt object's state somehow to be able to reload it later? (PySide2)
    Thanks for answers!

    1 Reply Last reply
    0
    • NiagarerN Offline
      NiagarerN Offline
      Niagarer
      wrote on last edited by Niagarer
      #2

      Thank you, but that wasn't really the question.
      From what I know, pickle should normally be able to 'pickle' all types that derive from python's object class, which isn't the case here.
      That doesn't mean it's not possible to do the same. It will probably come down to the fact, that Qt's c++ classes are partially not 'picklable' (how should one store a QColor() pointer f.ex.) so storing the values of the members probably isn't possible. But I don't know, maybe there is a way.

      SGaistS 1 Reply Last reply
      0
      • NiagarerN Niagarer

        Thank you, but that wasn't really the question.
        From what I know, pickle should normally be able to 'pickle' all types that derive from python's object class, which isn't the case here.
        That doesn't mean it's not possible to do the same. It will probably come down to the fact, that Qt's c++ classes are partially not 'picklable' (how should one store a QColor() pointer f.ex.) so storing the values of the members probably isn't possible. But I don't know, maybe there is a way.

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

        Hi,

        @Niagarer said in How to save/pickle Qt object's state:

        Thank you, but that wasn't really the question.
        From what I know, pickle should normally be able to 'pickle' all types that derive from python's object class, which isn't the case here.
        That doesn't mean it's not possible to do the same. It will probably come down to the fact, that Qt's c++ classes are partially not 'picklable' (how should one store a QColor() pointer f.ex.) so storing the values of the members probably isn't possible. But I don't know, maybe there is a way.

        This is just an educated guess but for one thing QObject based classes cannot be copied. However, not all Qt classes derived from QObject. QColor, to take your example, is such a class and is usually not allocated on the heap so I wouldn't be surprised that pickle might work on it. But I have not tested it yet.

        Therefore, for QObject based classes, a "clone" method is the usual way to do it in C++ when one would like a second object with the same properties. In case of pickling, like was already suggested, pickling the properties and creating a new object on the other side will likely be the simpler approach.

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

        NiagarerN 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          @Niagarer said in How to save/pickle Qt object's state:

          Thank you, but that wasn't really the question.
          From what I know, pickle should normally be able to 'pickle' all types that derive from python's object class, which isn't the case here.
          That doesn't mean it's not possible to do the same. It will probably come down to the fact, that Qt's c++ classes are partially not 'picklable' (how should one store a QColor() pointer f.ex.) so storing the values of the members probably isn't possible. But I don't know, maybe there is a way.

          This is just an educated guess but for one thing QObject based classes cannot be copied. However, not all Qt classes derived from QObject. QColor, to take your example, is such a class and is usually not allocated on the heap so I wouldn't be surprised that pickle might work on it. But I have not tested it yet.

          Therefore, for QObject based classes, a "clone" method is the usual way to do it in C++ when one would like a second object with the same properties. In case of pickling, like was already suggested, pickling the properties and creating a new object on the other side will likely be the simpler approach.

          NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on last edited by Niagarer
          #4

          @SGaist
          Hi,
          I'm sorry, I think I don't understand. Isn't that actually the problem, having to save property types that cannot be 'saved'? QColor actually does work, by the way, you are right, bad example by me :)
          For example, if I wanted to somehow save or copy or clone (whatever) a QGraphicsScene, I would also have to save the QGraphicsItems and Widgets, etc. placed in it. I am not aware of a way to save their states neither with pickle nor with JSON or something like that. If it is impossible to save these objects (like in a file), is there a way to clone them in PySide2 so that I can switch out my different versions of my scene for example later?

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

            That's up to you to implement the "cloning" or streaming of properties you want or need to get to the other side.

            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
            1
            • NiagarerN Niagarer

              @SGaist
              Hi,
              I'm sorry, I think I don't understand. Isn't that actually the problem, having to save property types that cannot be 'saved'? QColor actually does work, by the way, you are right, bad example by me :)
              For example, if I wanted to somehow save or copy or clone (whatever) a QGraphicsScene, I would also have to save the QGraphicsItems and Widgets, etc. placed in it. I am not aware of a way to save their states neither with pickle nor with JSON or something like that. If it is impossible to save these objects (like in a file), is there a way to clone them in PySide2 so that I can switch out my different versions of my scene for example later?

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

              @Niagarer
              To save & reload a gfx scene, I json-ize out the hierarchy with class names and whatever attributes I know the user/code has been allowed to change, which I am in control of; enough to allow me to reconstruct. If I had to do all properties, it would be quite a bit of code. It's not what you would like, but as far as I know it's what you have to do.

              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