[PyQt] Pickle QFlags ?

Kernc kerncece at gmail.com
Sat Nov 14 22:52:48 GMT 2015


Hi,

The PyQt docs mention that the enums from Qt namespace are pickleable
[1], which is thankfully indeed the case.

>>> from PyQt5.QtCore import Qt
>>> type(pickle.loads(pickle.dumps(Qt.LeftButton)))  # works
PyQt5.QtCore.Qt.MouseButton

What doesn't work, however, but isn't exactly obvious that it
shouldn't, is pickling QFlags, combinations of enum values, e.g.

>>> pickle.dumps(Qt.LeftButton | Qt.RightButton)
PicklingError: Can't pickle <class 'PyQt5.QtCore.MouseButtons'>:
attribute lookup MouseButtons on PyQt5.QtCore failed

QFlags, while a full-blown C++ class, still represents just a single
integer state, which should be trivial to store/restore.

With PicklingError being about "attribute lookup MouseButtons on
PyQt5.QtCore failed," and since MouseButton is in
PyQt5.QtCore.*Qt*.MouseButton, is this perhaps a bug?

Could I possibly make it work now, or could it possibly be fixed in a
few lines in sip code to make QFlags picklable?

Thanks.

[1]: http://pyqt.sourceforge.net/Docs/PyQt5/pickle.html


More information about the PyQt mailing list