Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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+



  • @Vadi2
    so why not set it anyway with the static function before you create the QApplication object then reset it as appropriate when you can get the information? All this can happen before you create your GUI objects right?



  • That won't work, you have to enable or disable it before the application is created.


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    @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.


  • Lifetime Qt Champion

    @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.



  • Just do it before the QApplication

    int main(int argc, char *argv[])
    {
        QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    With it on (bad):

    0_1549165072716_MainWindow_345.png

    With it off (good):

    0_1549165115372_MainWindow_346.png


  • Lifetime Qt Champion

    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!


Log in to reply