pythonsvgpyqtpyqt5hidpi

Using SVG icons on HiDPI displays in PyQt5


I would like to use high-resolution icons in my PyQt5 application. However, the following code snippet produces a very low-res rendering on my HiDPI macOS platform (the required icon can be downloaded here):

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction
from PyQt5.QtGui import QIcon


app = QApplication(sys.argv)
main = QMainWindow()
icon = QIcon("waves-24px.svg")
action = QAction(icon, "Test")
toolbar = main.addToolBar("toolbar")
toolbar.addAction(action)
toolbar.show()
main.show()
sys.exit(app.exec_())

This is what the result looks like (note the low resolution of the icon): enter image description here

What am I doing wrong?


Solution

  • Turns out there is a simple solution:

    app.setAttribute(Qt.AA_UseHighDpiPixmaps)