5
votes

It would be quite entertaining to find why having an instance of Qt object as a MyClass()'s attribute causes a two pages crash log. And how to avoid it:

import sys, os, pickle
from PyQt4 import QtGui

class MyClass(object):
    def __init__(self):
        self.myQtWidget=QtGui.QPushButton() 
        pass 

app = QtGui.QApplication(sys.argv)
myInstance=MyClass()
pickle.dump( myInstance, open( "save.mydata", "wb" ) )
1
I'd be interested to know why you need to pickle an instance of a QWidget. It seems to me like a case of the xy problem (see meta.stackexchange.com/questions/66377/what-is-the-xy-problem ). If you need a solution, it might be worth asking a new question explaining what you were trying to solve with the pickling solution (because as you can see below, pickling is not going to work) - three_pineapples
@three_pineapples. Agreed on your assessment, unless the OP doesn't really need to pickle the widgets, but rather just the other stuff that the class instance contains. If so, my comment belows offers a solution. But even so, this seems like a hackish workaround for what is likely a flawed design. - ekhumoro

1 Answers

6
votes

From the docs:

The following PyQt4 classes may be pickled.

QByteArray
QChar
QColor
QDate
QDateTime
QKeySequence
QLatin1Char
QLatin1String
QLine
QLineF
QMatrix
QPoint
QPointF
QPolygon
QRect
QRectF
QSize
QSizeF
QString
QTime

Also all named enums (QtCore.Qt.Key for example) may be pickled.