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

Here is what was working for me. You can set DPIawareness manually by giving a command line option at the instanciation of the QApplication.

The official documentation is here https://doc.qt.io/qt-5/highdpi.html (section DPI awareness).

According to the documentation, you can set the application to DPI Unaware (thus it will automatically scale but display will be blurred), or to System DPI Aware or to Per-Monitor Aware.

Here is a minimal example code for the instanciation of the QApplication to force High DPI, choose other value than 1 (0 or 2) to enable DPIUnaware or Per Monitor DPI Aware:

int main() 
{
   int argc = 3;
   char*argv[] = {(char*)"Appname", (char*)"--platform", (char*)"windows:dpiawareness=1";
   (void) new QApplication(argc, argv);
}

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);.

Tags:

C++

Qt

Highdpi