Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

highdpi

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?

like image 321
michnovka Avatar asked Mar 01 '16 04:03

michnovka


3 Answers

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

like image 195
Ribtoks Avatar answered Nov 20 '22 00:11

Ribtoks


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);
}
like image 39
Pat. ANDRIA Avatar answered Nov 19 '22 22:11

Pat. ANDRIA


With QT_AUTO_SCREEN_SCALE_FACTOR, the point size of the fonts are not changed, they're just scaled up from their original pixels, so they will never be smooth, only more bumpy.

Ref: http://doc.qt.io/qt-5.6/highdpi.html#high-dpi-support-in-qt "This will not change the size of point sized fonts"

You need to use QT_SCALE_FACTOR instead to rescale your app, not just rescale its pixels.

like image 2
G Huxley Avatar answered Nov 19 '22 23:11

G Huxley