views:

93

answers:

2

I realize specific questions like this aren't great, but I've spent several days trying to puzzle this out. Hopefully someone here can help.

This python code using PyQt4 causes a segmentation fault:

data = """<?xml version="1.0" ?>
          <svg height="1000" width="2000">
              <text>blah</text>
          </svg>"""

svg = QSvgRenderer(QByteArray(data))
qim = QImage(int(width), int(height), QImage.Format_ARGB32)
painter = QPainter()

painter.begin(qim)
svg.render(painter)
painter.end()

qim.save('test2.png')

The line that causes the fault is svg.render(painter).

The fault points at libQtGui.so (so something in QPainter or QImage).

svg.isValid() returns True, and qim.isNull() returns False.

A: 

Try making it draw on a QPixmap instead of a QImage.

Qt does cause segfaults once in a while, I usually just code around them. Maybe you could rasterize this SVG in Gimp and just load that.

Nathan
I'll give it a go, but why?
colinmarc
I get "QPixmap: Must construct a QApplication before a QPaintDevice". Also, the docs say "QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen."
colinmarc
In response to your edit, the reason I'm doing this is to programatically rasterize svg. I don't care about creating a Qt application. I would use `rsvg`, but it doesn't support `<textPath>` elements. See http://stackoverflow.com/questions/3119882/
colinmarc
I've never seen a segfault in Qt that wasn't my fault or very obvious (e.g. when they changed the order of initialization generated by `uic`). Note that it's possible, but unlikely. A stacktrace would be useful in this case.
Kaleb Pederson
@colinmarc - `QImage` should be fine whereas `QPixmap` does require a GUI. You should be able to use the SVG Viewer example (http://doc.trolltech.com/4.6/painting-svgviewer.html) to verify that Qt's SVG handling does what you need.
Kaleb Pederson
Because I thought this thread might be related, and because it would be easy. (http://lists.trolltech.com/qt-interest/2008-08/thread00397-0.html)
Nathan
A: 

With only a minor change to make that run (defining width and height), it works for me. Note that I don't see any text but if I swap out data to something I know is valid it works perfectly. Here's my full code:

#!/usr/bin/env python

from  PyQt4.QtGui import *
from  PyQt4.QtCore import *
from PyQt4.QtSvg import *
import sys

if __name__ == '__main__':

    app = QApplication(sys.argv)

    data = """... (my valid svg) ..."""

    svg = QSvgRenderer(QByteArray(data))
    qim = QImage(int(2000), int(1000), QImage.Format_ARGB32)                                                                                                                                                                                 
    painter = QPainter()

    painter.begin(qim)
    svg.render(painter)
    painter.end()

    print "null:", qim.isNull()
    qim.save('test2.png')
Kaleb Pederson
the part I was missing was declaring a `QApplication`. Pretty frustrating, especially since that requirement isn't mentioned anywhere in the `QImage`, `QPainter`, or `QSvgRenderer` documentation that I read.
colinmarc