1

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?

cbrnr
  • 1,564
  • 1
  • 14
  • 28

2 Answers2

5

Turns out there is a simple solution:

app.setAttribute(Qt.AA_UseHighDpiPixmaps)
cbrnr
  • 1,564
  • 1
  • 14
  • 28
0

When loading icon from SVG, QT renders it into pixmap first. By default, dimensions of the pixmap are picked from svg page size, namely width and height attributes of svg root element. For your svg renderer produces 24px x 24px pixmap. So, Qt loads you picture into low resolution buffer first, and scales ins content after that. To fix this, try to enlarge svg width and height attributes to increase page size and scale image. In Inkscape, page size settings are located in File > Document properties > Page tab.

  • I can increase the page size to e.g. 240x240, but the actual image doesn't scale - how can I do that? – cbrnr Oct 17 '19 at 13:49
  • Enclose path elements in svg xml into group element and add transformation: `` – Alexander Zavertan Oct 17 '19 at 15:24
  • The icon is still rendered in the same low resolution (I even tried `scale(100)` and `2400` in the `width`, `height`, and `viewBox` fields). – cbrnr Oct 21 '19 at 08:49