Automatic rescaling of an application on high-dpi Windows platform?
Asked Answered
M

5

22

I'm writing a Qt application that needs to run on high-dpi Windows (192dpi instead of 96dpi).

Unfortunately the Qt framework does not have support for high-dpi yet (at least on Windows), so my application and all its elements looks half the size it should.

Is there any way to force/simulate automatic upscaling of such apps by Windows?

Marj answered 23/6, 2014 at 13:35 Comment(0)
P
11

Applications that use fixed coordinates and sizes will look small on high-DPI resolutions. Although even if using layouts there are some issues regarding element and font sizes and margins. Fortunately there is support for high-DPI displays since Qt 5.4 as there has been many high-DPI issue fixes.

An application on Windows can assume one of the following levels of "DPI Awareness" (From the Qt documentation) :

  • DPI Unaware: This level has been introduced in Windows-Vista. Windows will pretend to the application that it is running on a standard display of 96 DPI of 1920x1080 and scale the application accordingly. It is intended to accommodate older applications designed for low DPI displays. Some artifacts may result from this type of scaling.
  • System-DPI Aware: This level has been introduced in Windows-Vista. It differs from Per-Monitor DPI Aware only when multiple monitors are connected. Windows will calculate a scaling suitable for all connected monitors.
  • Per-Monitor DPI Aware: This level has been introduced in Windows 8.1. Windows does not perform any scaling at all.

Also it states that :

Qt applications by default are Per-Monitor DPI Aware on Windows 8.1 or System-DPI Aware on older versions of Windows. As of Qt 5.4, the level can be specified by passing a parameter to the platform plugin (see Using qt.conf):

<application> -platform windows:dpiawareness=0,1,2

You can read more information here.

In general to have a good UI on high-DPI displays, consider the following :

  • Use the latest version of Qt
  • Use layouts and avoid fixed sizes (unless you calculate scaling ratios on your own)
  • Make appropriate DPI-related settings depending on your application needs, for example set Qt::AA_UseHighDpiPixmaps attribute if you work with QPainter and pixmaps, or calculate a scaling ratio for adjusting certain element sizes in special situations.
Piscatelli answered 19/2, 2015 at 4:47 Comment(0)
Z
10

Qt fully supports high DPI monitors from Qt 5.6 onward, via attribute or environment variable (except on OS X where support is native). For the attribute method, use:

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // DPI support

    QApplication app(argc, argv);   
    return app.exec();
}

or set the system environment variable:

QT_AUTO_SCREEN_SCALE_FACTOR=1

I've tested both methods on windows 10 with a high-DPI surfacebook monitor and the results are scaled properly as expected.

Zelikow answered 17/3, 2016 at 11:9 Comment(1)
Didn't change anything.Periodate
C
7

Here is the quickest way to get the issue solved on Windows.

Next to the executable, create a qt.conf file (if not already there) and add the following:

[Platforms]
WindowsArguments = dpiawareness=0

The window will appear blurry when scaled up. The big advantage of this solution is that Windows does the scaling, not Qt. Therefore the occurence of artifacts is minimized. Furthermore, this can apply to an already-distributed app as it does not require a rebuild.

Of course, this is not the most pleasant result but the quickest to get you out of trouble in short term, letting you develop the "real" DPI-aware version without pressure.

Coldiron answered 16/5, 2018 at 10:2 Comment(0)
C
1

I' am using Qt 4.8. First, you should use layouts. My goal was to prevent user to resize dialogs, forms etc. too.

I achieved correct display results on different DPI by put this code in dialog constructor:

adjustSize();
setFixedSize(size());

First line adjustSize() adjust size of dialog to fit its content. Second line setFixedSize(size()) fixes size of dialog after adjusting its size to content and prevent users to resize it. You haven't to set size policies.

Crummy answered 15/4, 2017 at 19:25 Comment(0)
B
1

I' am using Qt 4.8.7. This is my code.

static float dpiX = (float)0;
inline int autoDpiSize(int inSize)
{
    if (dpiX == (float)0)
    {
        HDC screen = GetDC(0);
        dpiX = static_cast<float >(GetDeviceCaps(screen, LOGPIXELSX)) / 96.0;
        ReleaseDC(0, screen);
    }
    return dpiX * inSize;
}
someWidget->setFixedHeight(autoDpiSize(30));
Buss answered 5/6, 2020 at 2:19 Comment(2)
thanks for the tip, but might I ask why you still use 4.8 in 2020 ? It's almost a decade old.Marj
because we have an old program and it needs to be compatible with XP, even 2000.Buss

© 2022 - 2024 — McMap. All rights reserved.