How to check screen dpi before a QApplication exists?
-
AA_EnableHighDpiScaling
needs to be set before a QApplication is started, but on Linux setting this property for a normal non-DPI display will cause everything to be 2x the size on Qt versions under 5.12.Thus, I need to check the screen size before setting this property, but the screen size isn't available before the application...
How can I work around this Qt bug? I support Qt 5.7+
-
-
That won't work, you have to enable or disable it before the application is created.
-
Hi
I have no hires screen to test on so just asking.
You are saying that#include "mainwindow.h" #include <QApplication> #include <QScreen> int main(int argc, char *argv[]) { QApplication a(argc, argv); if ( a.screens().at(0)->geometry().width() > 2000) // 2000 is just random value. QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); else { QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, false ); } MainWindow w; w.show(); return a.exec(); }
Does not fix the issue , turn on/off the scaling ?
-
Don't need a high-res display: the bug is on normal, non-high res displays. Using Qt 5.6 - 5.11, enable
AA_EnableHighDpiScaling
and watch everything blow up in size :(and yes, setting the property after the initialisation doesn't seem to affect anything.
-
@Vadi2
hi
and i assume that Qt::AA_DisableHighDpiScaling suffer the same thing as not change anything
if done in main ? -
I'm not sure what do you mean - but I've already tried setting
Qt::AA_DisableHighDpiScaling
after my application was created and it was too late. -
@Vadi2
Yes that was what i meant.
It seems to be changed in Qt5.12 as i cant get it to show as (too) big.When using Qt5.6, how do you enable AA_EnableHighDpiScaling ?
Just in case i want to test on that version. -
-
Hi
Oddly enough i installed Qt5.6 and tried on windows 10 but
didnt change the Widgets even with (Qt::AA_EnableHighDpiScaling, true);Anyway, i was wondering if a workaround would be
int main(int argc, char *argv[]) { bool EnableHighDpiScaling = false; { QApplication a(argc, argv); if ( a.screens().at(0)->geometry().width() > 1090) // check somehow. DPI i assume EnableHighDpiScaling = true; } QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, EnableHighDpiScaling); QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
It cost an extra construction of a QApplication but the added startup time should be negligible ?
-
This might be a Linux-only bug, as Windows does HiDPI differently (as does macOS).
I'll try your suggestion!
-
Works, thanks a lot!