I want to display an svg
file using PySide. But I would prefer not having to create a file.svg
and then load it using QSvgWidget
Rather I just have the file contents of the file.svg
contents already stored in a variable as a string.
How would I go about directly loading the SVG from the variable rather than form the file.
Best
You need to convert the string to a QByteArray.
From the QSvgWidget.load documentation:
PySide6.QtSvgWidgets.QSvgWidget.load(contents)
PARAMETERS:
contents –
PySide6.QtCore.QByteArray
Here's a full demo:
from PySide6.QtWidgets import QApplication
from PySide6.QtSvgWidgets import QSvgWidget
from PySide6.QtCore import QByteArray
import sys
svg_string="""<svg width="50" height="50">
<circle cx="25" cy="25" r="20"/>
</svg>"""
svg_bytes = QByteArray(svg_string)
app = QApplication(sys.argv)
svgWidget = QSvgWidget()
svgWidget.renderer().load(svg_bytes)
svgWidget.show()
sys.exit(app.exec_())