13

I'm looking for a simple and reliable way for inserting an SVG image in a PyQt window. More precisely, I'm wondering if it's possible to have an SVG image applied to a QLabel. This can be done for PNG images (see code bellow).

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)

label = QtGui.QLabel()
pixmap = QtGui.QPixmap('my_image.png')
label.setPixmap(pixmap)
label.show()

sys.exit(app.exec_())

Edit:

I need to keep the vector aspect of the SVG image for resizing purpose. So converting the image to PNG before displaying it isn't helping.

Roger Hache
  • 405
  • 1
  • 5
  • 17
  • 1
    I use GTK rather than Qt, but [this](http://stackoverflow.com/questions/8551690/how-to-render-a-scaled-svg-to-a-qimage) looks relevant. – PM 2Ring Feb 01 '16 at 12:06
  • Apart from the fact that the post you linked shows solution for Qt (not PyQt), it is used to convert an SVG to a PNG. I may have been not clear enough but it's not what I need. i'd like to keep the vector format so that I can resize the image. – Roger Hache Feb 01 '16 at 13:11
  • 1
    Sorry. I hoped it would contain enough info for you to adapt it to Python, but I understand doing that's a bit messier than having a totally Python-based solution: I know it's painful looking through C-based stuff when I'm trying to do GTK stuff in Python. But anyway, I _suspect_ that you'll have to display the SVG in a Pixmap, which means that you can't avoid the vector to bitmap conversion. But your code can re-render the SVG in response to scaling commands; that's what I do with SVGs in GTK. – PM 2Ring Feb 01 '16 at 13:20
  • Ahh, I didn't get it the first time: the code render the SVG to given scale during runtime. This is actually a great solution! I'll try to figure out how to write this for python (PyQt). – Roger Hache Feb 01 '16 at 13:30
  • Good luck! If you get stuck, you can add the new code to your question, or just ask a fresh question, possibly linking back to this one. Or if you write a nice solution post it here as an answer: you might pick up a few points, and it may help future readers. – PM 2Ring Feb 01 '16 at 13:35

1 Answers1

25

QLabel only can load a pixmap. If You need a vector-graphic use QtSvg.QSvgWidget() and load the svg-file without converting:

import sys
from PyQt4 import QtGui, QtSvg

app = QtGui.QApplication(sys.argv) 
svgWidget = QtSvg.QSvgWidget('Zeichen_123.svg')
svgWidget.setGeometry(50,50,759,668)
svgWidget.show()

sys.exit(app.exec_())

or render to any subclass of QPaintDevice by QtSvg.QSvgRenderer directly, e.g. to QLabel:

app = QtGui.QApplication(sys.argv) 

widget = QtGui.QLabel()
widget.setGeometry(50,200,500,500)
renderer =  QtSvg.QSvgRenderer('Zeichen_123.svg')
widget.resize(renderer.defaultSize())
painter = QtGui.QPainter(widget)
painter.restore()
renderer.render(painter)
widget.show()

sys.exit(app.exec_())

get Zeichen_123.svg here

a_manthey_67
  • 4,046
  • 1
  • 17
  • 28
  • This is exactly what I was looking for! Thank you! I'm just having an issue: the SVG image I want to display contains text and it won't show up. Instead of the letters I have black boxes... Any idea? – Roger Hache Feb 03 '16 at 15:40
  • 2
    I think, this could be a new question. before you ask it with code from your svg-file: Do other viewers show the svg-file correctly? are simple -tags in the svg-file or -tags? see: http://stackoverflow.com/questions/19391197/svg-inkscape-generated-file-does-not-show-flowroot-objects-on-browser – a_manthey_67 Feb 03 '16 at 18:46
  • The "black box in place of letters" problem may be due to Qt using the Tiny SVG 1.2 spec. I had this issue as well, see [here](https://www.kdab.com/the-qt-less-traveled/) for more details. – tdpu Nov 25 '22 at 18:25