SVG icons are stretched (not drawn pixel-perfect) with display scaling enabled
-
I'm simply setting a QAction icon from SVG file, and I emulate display scaling with setting env var
QT_SCALE_FACTORto 2.0.
The icon looks like this, obviously rendered at 1x and then stretched. How can I work around this? Why does Qt do this in the first place with HiDpi support enabled?
Tested with Qt 5.15.2.
Here's the complete repro code:
int main(int argc, char *argv[]) { qputenv("QT_SCALE_FACTOR", "2.0"); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); QApplication a(argc, argv); a.setStyleSheet("*{background: #191919; color: #DDDDDD;}"); QMainWindow w; QToolBar* tb = new QToolBar; w.addToolBar(tb); QPixmap icon("help.svg"); icon.setDevicePixelRatio(2.0); // This appears to change nothing tb->addAction(new QAction(icon, "text")); w.show(); return a.exec(); }Here's the sample project - an SVG icon and the code above. Since I can't attach a ZIP directly, here's a link to cloud storage. Turn off shadow build for the icon to load successfully.
-
I'm simply setting a QAction icon from SVG file, and I emulate display scaling with setting env var
QT_SCALE_FACTORto 2.0.
The icon looks like this, obviously rendered at 1x and then stretched. How can I work around this? Why does Qt do this in the first place with HiDpi support enabled?
Tested with Qt 5.15.2.
Here's the complete repro code:
int main(int argc, char *argv[]) { qputenv("QT_SCALE_FACTOR", "2.0"); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); QApplication a(argc, argv); a.setStyleSheet("*{background: #191919; color: #DDDDDD;}"); QMainWindow w; QToolBar* tb = new QToolBar; w.addToolBar(tb); QPixmap icon("help.svg"); icon.setDevicePixelRatio(2.0); // This appears to change nothing tb->addAction(new QAction(icon, "text")); w.show(); return a.exec(); }Here's the sample project - an SVG icon and the code above. Since I can't attach a ZIP directly, here's a link to cloud storage. Turn off shadow build for the icon to load successfully.
Turns out that creating icons via
QPixmapis an anti-pattern. It is drawn correctly if constructingQIcondirectly:QIcon icon("icon.svg"); -
V Violet Giraffe has marked this topic as solved on