Qt SVG generating and parsing asymmetry: you can persist a QPainterPath to QByteArray but not unpersist.
-
I want to persist a QPainterPath.
I have found that you can generate a QByteArray from a QPainterPath using QSvgGenerator. (paint the QPainterPath onto the QSvgGenerator surface whose output device is a QBuffer subclass of QIODevice.) Then persist the QByteArray (I happen to be using PySide and Python's pickle.)
But I have not found a symmetrical way to unpersist or restore a QPainterPath from a QByteArray. QSvgRenderer obviously parses SVG but does not offer access to the parse tree, will only renderer it to a QPaintDevice. But for example, QGraphicsScene is not a QPaintDevice, and there are no other QPaintDevices that offer structured access to elements.
Am I missing something?
(I found one another post on QtForum wishing for this feature.)
I suppose it is not difficult to parse SVG and create QGraphicsItems. Anyone know of existing code?
-
Answering my own question: QPainterPath implements << and >> for serializing.
@
def _glyphData(self):
'''
Pickleable QPainterPath data. QGraphicsPathItem does not pickle.
'''aBuffer = QBuffer() aBuffer.open(QIODevice.ReadWrite) stream = QDataStream(aBuffer) stream << self.path() array = aBuffer.data() return array
def _loadFromData(self, data):
path = QPainterPath() aBuffer = QBuffer(data) aBuffer.open(QIODevice.ReadOnly) stream = QDataStream(aBuffer) stream >> path self.setPath(path)@
-
But now I found this doesn't work, in the sense that the path deserialized has about 3x more elements than the path which was serialized? When viewed, the path has the same shape, but the line is about twice as thick and is more jaggy! I don't understand it yet. I would appreciate any insights.