Rendering SVG images in QIcons natively not supported
-
The problem is that the SVG images imported into resources.qrc are not properly displayed by the Ui_MainWindow class without it's modifications.
- I am using the QT Designer Qt version 6.3.1
- I am creating the resources.qrc file and filling it with .svg icons
- I am creating my application in QT Designer, all icons are displayed correctly at preview
- I save the .ui file
- I transform it into .py file with:
$ pyside6-uic ui_file_name.ui > python_file_name.py
- I transform the resources file into .py file with:
$ pyside6-rcc icons.qrc -o icons_rc.py
And my application does not display the svg icons at runtime...
After having researched the web, I have noticed that after transforming the .ui file into .py it calls for .svg resources like so:
icon = QIcon() icon.addFile(u":/icons/icons/book-open.svg", QSize(), QIcon.Normal, QIcon.Off)
Changing it manually to the below solves the problem:
from PySide6 import QtSvg from PySide6 import QtGui svg_renderer = QtSvg.QSvgRenderer(":/icons/icons/book-open.svg") image = QtGui.QImage(64,64, QtGui.QImage.Format_ARGB32) image.fill(0x00000000) svg_renderer.render(QtGui.QPainter(image)) pixmap = QtGui.QPixmap.fromImage(image) icon = QIcon(pixmap)
But it has to be done to each one of the dozens of my icons every time I update the .ui file with QT Designer...
I was wandering - is there a more proper way to generate .ui files from the QT designer or to transform them into .py with native support for .svg resources? There must be some better way than my reiterative manual hassle.
-
Hi,
Are you importing your resource file somewhere in your code ?
-
@DanilZ said in Rendering SVG images in QIcons natively not supported:
native support for .svg resources
QIcon supports SVG images via an "icon engine" plugin.
I would check if your plugin is correctly installed/deployed (for example,
plugins/iconengines/qsvgicon.dll
) -
I am a C++ guy and don't know exactly how this translates to Python. However, to be able to use SVG icons in a qmake-based project I need to add
QT += svg
. You should have something similar in your project to get it working. -
Simply import the library :
from PySide6 import QtSvg