HiDPI and SVG icon resolution
-
@cle1109 That is unfortunate that the workaround does not work on MacOS. I have one more suggestion you could try. Instead of
pixmap = QPixmap(toolbar.iconSize()) pixmap.setDevicePixelRatio(main.devicePixelRatio())
just try
pixmap = QPixmap(toolbar.iconSize() * main.devicePixelRatio())
There is a slight chance that using
pixmap.setDevicePixelRatio
does not work properly with SVG on MacOS. In this case this would create a larger icon which is then scaled down when drawing it on the toolbar. -
@SimonSchroeder I think there is something wrong with the code because like in the first version, I get a more or less random icon with the second version as well. It looks like either the conversion
icon = QIcon(pixmap)
doesn't work or one of the preceding steps. -
I did
pixmap.save("pixmap.png")
after thepainter
stuff and the resulting PNG doesn't look right. So either the painter doesn't paint correctly into the pixmap or the image is not read in correctly. Can I debug this further to find out what the problem is? -
To me, this means that the painter does not paint correctly. I'm out of ideas.
Maybe, one last thing to check: I am not sure how a QPython project would be set up. For C++ I am using
qmake
. One thing I noticed is that in the end SVG icons are working. However, it would be more correct to add Qt's SVG module, i.e. I would add the lineQT += svg
to my
qmake
project file. Is there something similar for Python? -
I don't think this is necessary in Python. It is sufficient to import the required packages. In the example, I don't explicitly require the
PyQt5.QtSvg
module, but I don't think this is a problem (the stuff I'm using should automatically use functions from that module if needed).I will rewrite my example in C++ to see if this is a problem specific to the Python bindings. That way, it will be easier to decide where to file the bug report (since this is working on Windows and Linux). I'll keep you posted.
-
I tried this example in C++ and the result is exactly the same. The SVG icon is rendered in a very low resolution.
#include <QApplication> #include <QMainWindow> #include <QIcon> #include <QAction> #include <QToolBar> int main(int argc, char **argv) { QApplication app(argc, argv); QMainWindow window; QIcon icon("waves-24px.svg"); QAction action(icon, "Test"); QToolBar *toolbar = new QToolBar(&window); toolbar->addAction(&action); window.addToolBar(toolbar); window.show(); return app.exec(); }
-
You'll need to use the trick with the QPixmap and QPainter in C++ as well. In the background Python and C++ use the exact same functionality, so I wouldn't expect any other behavior without the workaround. (I have experienced this exact problem in C++ multiple times.)
For your
main.pro
you need to add thesvg
module also:... QT = core gui widgets svg ...
This is what I actually meant with my last post.
-
You'll need to use the trick with the QPixmap and QPainter in C++ as well. In the background Python and C++ use the exact same functionality, so I wouldn't expect any other behavior without the workaround. (I have experienced this exact problem in C++ multiple times.)
I get exactly the same result as in Python (the icon is just a random pixel pattern).
Yes, I know what you meant, I tried adding
svg
but it didn't make any difference. Same for the workaround. So in conclusion, this seems to be a Qt bug on macOS which also affects PyQt5.