更改显示的 DPI 缩放大小会使 Qt 应用程序的字体大小变得更大

2021-12-09 00:00:00 user-interface qt resolution c++ dpi

我使用 Qt 创建了一些 GUI 应用程序.我的 GUI 应用程序包含按钮和单选按钮等控件.当我运行应用程序时,按钮内的按钮和字体看起来很正常.当我将显示的 DPI 缩放大小从 100% 更改为 150% 或 200% 时,无论分辨率如何,控件的字体大小都会变大,但控件大小(按钮、单选按钮)不会.由于这个原因,控件中的文本被截断了.请看附图.

I have created some GUI application using Qt. My GUI application contains controls like push button and radio button. When I run the application, buttons and fonts inside button looks normal. When I change the DPI scaling size of display from 100% to 150% or 200%, font size of controls rendered bigger but not control size (pushbutton, radio button) irrespective of resolution. Due to this the text inside controls were cut off. please see the attached image.

Qt 应用程序在 DPI 缩放大小设置为 100% 时的外观

Qt application look when DPI scaling size set to 100%

Qt 应用程序在 DPI 缩放大小设置为 200% 时的外观

Qt application look when DPI scaling size set to 200%

我也在一些平板电脑上运行我的应用程序.在平板电脑中,DPI 比例值应大于 150%,否则所有内容都会显示得很小.

I am running my application in some tablets also. In tablets, DPI scale value should be more than 150% else everything will be shown very small.

我在网上搜索在 Qt 中创建 UI 应用程序,而不管分辨率和 DPI 比例值,但没有运气.所以我在这里发布我的任务.请告诉我是否有办法摆脱这种情况.

I searched in the web for creating UI application in Qt irrespective of resolution and DPI scale value but no luck. So I am posting my questing here. Please let me know if there is some way to get rid of this.

推荐答案

从 Qt 5.6 开始启用高 DPI 支持.

High DPI support is enabled from Qt 5.6 onward.

在应用程序源代码中设置 QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling) 允许自动高 DPI 缩放.

Setting QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling) in your application source code allows automatic high-DPI scaling.

注意:要使用属性方法,您必须在创建QApplication 对象之前设置属性:

NOTICE: To use the attribute method, you must set the attribute before you create your QApplication object:

#include <QApplication>

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

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

相关文章