How to save/pickle Qt object's state
-
wrote on 19 Mar 2020, 11:33 last edited by Niagarer
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! -
wrote on 19 Mar 2020, 17:03 last edited by 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. -
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.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.
-
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.
wrote on 22 Mar 2020, 06:29 last edited by 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? -
That's up to you to implement the "cloning" or streaming of properties you want or need to get to the other side.
-
@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?wrote on 23 Mar 2020, 13:26 last edited by JonB@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/6