QT High DPI Support on Windows

2019-04-29 21:05发布

According to documentation here http://doc.qt.io/qt-5/highdpi.html QT 5.4+ introduces high DPI support. However, either I’m missing something fundamental or the current support is still in very early stages. I’m writing a brand new application so I have a chance to do it right from the ground up. I understand that I would have to use layouts instead of fixed positioning etc, but there always going to be cases in which I would have to specify, for example a minimum/maximum size of a control. I can specify them in the editor, but these are device pixels. So if I change my Windows settings to use 150% DPI then min/max values in the editor would be too small. Of course I can obtain that ratio and adjust all the required values in code, but then what kind of high DPI support does QT give for me if I have to do everything by hand? I mean how is it different to pre QT 5.4?

Then an interesting one is QT_DEVICE_PIXEL_RATIO environment variable. It does exactly what I need, it multiplies all pixels set in editor by a factor. But why is it an environment variable and not a per application setting? Why does it only support integer values of 2, 3 etc, since we know that Windows has settings like 125, 150% etc. and why couldn’t it automatically read the Windows setting and set itself to that value?

2条回答
等我变得足够好
2楼-- · 2019-04-29 21:35

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

More information on the Qt blog

查看更多
萌系小妹纸
3楼-- · 2019-04-29 21:38

I must response the answer from @Nicolas Holthaus that the way you enable Qt::AA_EnableHighDpiScaling may not be absolutely right. Since it will round the user DPI setting. Eg. Windows DPI setting be 150%, the result will be 200% for font and size, while 125% will be 100%.

The right way to do correct DPI scaling is set environment variable QT_SCALE_FACTOR. For the same example, if DPI setting is 150%, set QT_SCALE_FACTOR with value 1.5. Then the result will be exactly 150% in font and size.

See the qt official document http://doc.qt.io/qt-5/highdpi.html and you will find

QT_SCALE_FACTOR [numeric] defines a global scale factor for the whole application, including point sized fonts.

查看更多
登录 后发表回答