Search code examples
c++qthighdpi

How to get sharp UI on high dpi with Qt 5.6?


I am developing on a 4k monitor and its a pain...

Finally I managed to set up QtDesigner and then ecountered this issue:

When you use QT_AUTO_SCREEN_SCALE_FACTOR=1 and compile an app with radiobutton and other basic widgets, it looks scaled on a 4k screen. Otherwise the dimensions of controls are correct and as expected, its just not sharp, but rather pixelated.

I am running on Windows 10 Home 64bit on 4k screen with 200% DPI zoom, using Qt 5.6 RC msvc2015 64bit and tried with achieving the same results using

QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

dcreenshot

If I use

QGuiApplication::setAttribute(Qt::AA_DisableHighDpiScaling);

the controls are sharp, text size is ok BUT all dimensions are much smaller.

How do I make controls sharp on high DPI screen?


Solution

  • As Qt documentation says:

    Use QT_AUTO_SCREEN_SCALE_FACTOR to enable platform plugin controlled per-screen factors.
    QT_SCREEN_SCALE_FACTORS to set per-screen factors.
    QT_SCALE_FACTOR to set the application global scale factor.
    

    You can try doing what Qt Creator is doing:

    static const char ENV_VAR_QT_DEVICE_PIXEL_RATIO[] = "QT_DEVICE_PIXEL_RATIO";
    if (!qEnvironmentVariableIsSet(ENV_VAR_QT_DEVICE_PIXEL_RATIO)
            && !qEnvironmentVariableIsSet("QT_AUTO_SCREEN_SCALE_FACTOR")
            && !qEnvironmentVariableIsSet("QT_SCALE_FACTOR")
            && !qEnvironmentVariableIsSet("QT_SCREEN_SCALE_FACTORS")) {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    }
    

    Basically important thing is the last line QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);.