In a Python3/QT5 application, I'm trying to show a SVG image built originally as a string. I need to manipulate this SVG image (e.g. change its color), so I have the string changing over time. Here is a minimal working example:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtSvg import QSvgWidget
svg_str = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="300" height="300" viewBox="0 0 300 300" id="smile" version="1.1">
<path
style="fill:#ffaaaa"
d="M 150,0 A 150,150 0 0 0 0,150 150,150 0 0 0 150,300 150,150 0 0 0
300,150 150,150 0 0 0 150,0 Z M 72,65 A 21,29.5 0 0 1 93,94.33
21,29.5 0 0 1 72,124 21,29.5 0 0 1 51,94.33 21,29.5 0 0 1 72,65 Z
m 156,0 a 21,29.5 0 0 1 21,29.5 21,29.5 0 0 1 -21,29.5 21,29.5 0 0 1
-21,-29.5 21,29.5 0 0 1 21,-29.5 z m -158.75,89.5 161.5,0 c 0,44.67
-36.125,80.75 -80.75,80.75 -44.67,0 -80.75,-36.125 -80.75,-80.75 z"
/>
</svg>
"""
# ==========================================
with open('smile.svg', 'w') as f:
f.write(svg_str)
# ==========================================
app = QApplication(sys.argv)
svgWidget = QSvgWidget('smile.svg')
svgWidget.setGeometry(100,100,300,300)
svgWidget.show()
sys.exit(app.exec_())
My problem is that I couldn't find a way around the need to save the file in order to instantiate a QSvgWidget object from the string itself. I don't want to save files indiscriminately and I couldn't find a way to load xml information directly to a QSvgWidget object...
I found a solution closest to my desires, and it looks like this:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtSvg import QSvgWidget, QSvgRenderer
from PyQt5.QtCore import QXmlStreamReader
from PyQt5.QtGui import QPainter
svg_str = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="300" height="300" viewBox="0 0 300 300" id="smile" version="1.1">
<path
style="fill:#ffaaaa"
d="M 150,0 A 150,150 0 0 0 0,150 150,150 0 0 0 150,300 150,150 0 0 0
300,150 150,150 0 0 0 150,0 Z M 72,65 A 21,29.5 0 0 1 93,94.33
21,29.5 0 0 1 72,124 21,29.5 0 0 1 51,94.33 21,29.5 0 0 1 72,65 Z
m 156,0 a 21,29.5 0 0 1 21,29.5 21,29.5 0 0 1 -21,29.5 21,29.5 0 0 1
-21,-29.5 21,29.5 0 0 1 21,-29.5 z m -158.75,89.5 161.5,0 c 0,44.67
-36.125,80.75 -80.75,80.75 -44.67,0 -80.75,-36.125 -80.75,-80.75 z"
/>
</svg>
"""
# ==========================================
class QSvgWidget_from_string(QSvgWidget):
def __init__(self, strSVG):
super().__init__()
self.strSVG = strSVG
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
svg_render = QSvgRenderer(QXmlStreamReader(self.strSVG))
qp.restore()
svg_render.render(qp)
qp.end()
# ==========================================
app = QApplication(sys.argv)
svgWidget = QSvgWidget_from_string(svg_str)
svgWidget.setGeometry(100,100,300,300)
svgWidget.show()
sys.exit(app.exec_())
But I'm not satisfied because I need to expand QSvgWidget class just to instantiate it from a xml string. My question is: Is there any way of doing this without having to resort to QPaint and paintEvent?
You can get the QSvgRenderer used by the QSvgWidget to render the SVG. It allows to load data from a file, byte array or a stream:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtSvg import QSvgWidget, QSvgRenderer
svg_str = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="300" height="300" viewBox="0 0 300 300" id="smile" version="1.1">
<path
style="fill:#ffaaaa"
d="M 150,0 A 150,150 0 0 0 0,150 150,150 0 0 0 150,300 150,150 0 0 0
300,150 150,150 0 0 0 150,0 Z M 72,65 A 21,29.5 0 0 1 93,94.33
21,29.5 0 0 1 72,124 21,29.5 0 0 1 51,94.33 21,29.5 0 0 1 72,65 Z
m 156,0 a 21,29.5 0 0 1 21,29.5 21,29.5 0 0 1 -21,29.5 21,29.5 0 0 1
-21,-29.5 21,29.5 0 0 1 21,-29.5 z m -158.75,89.5 161.5,0 c 0,44.67
-36.125,80.75 -80.75,80.75 -44.67,0 -80.75,-36.125 -80.75,-80.75 z"
/>
</svg>
"""
svg_bytes = bytearray(svg_str, encoding='utf-8')
app = QApplication(sys.argv)
svgWidget = QSvgWidget()
svgWidget.renderer().load(svg_bytes)
svgWidget.setGeometry(100,100,300,300)
svgWidget.show()
sys.exit(app.exec_())
More info here: http://doc.qt.io/qt-5/qsvgrenderer.html
If you do not need QSvgWidget e.g. you would like to get QImage (from which you can have QPixmap and QIcon) you can use:
qimage = QtGui.QImage.fromData(svg_bytes)
Make sure that svg is at supportedImageFormats.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With