Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt SVG generating and parsing asymmetry: you can persist a QPainterPath to QByteArray but not unpersist.

Qt SVG generating and parsing asymmetry: you can persist a QPainterPath to QByteArray but not unpersist.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 1.8k 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.
  • B Offline
    B Offline
    bootchk
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bootchk
      wrote on last edited by
      #2

      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)@
      
      1 Reply Last reply
      0
      • B Offline
        B Offline
        bootchk
        wrote on last edited by
        #3

        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.

        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