如何使用 Qt 5.6 在高 dpi 上获得锐利 UI?
How to get sharp UI on high dpi with Qt 5.6?
我正在 4k 显示器上开发,这很痛苦...
最后我设法设置了 QtDesigner,然后遇到了这个问题:
当您使用 QT_AUTO_SCREEN_SCALE_FACTOR=1
并使用单选按钮和其他基本小部件编译应用程序时,它在 4k 屏幕上看起来是按比例缩放的。
否则控件的尺寸是正确的,正如预期的那样,它只是不清晰,而是像素化。
我 运行 在 Windows 10 Home 64bit 4k 屏幕上使用 200% DPI 缩放,使用 Qt 5.6 RC msvc2015 64bit 并尝试使用
获得相同的结果
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
如果我用
QGuiApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
控件清晰,文字大小还可以,但所有尺寸都小得多。
如何在高 DPI 屏幕上使控件更清晰?
使用QT_AUTO_SCREEN_SCALE_FACTOR,字体的磅值没有改变,它们只是从原来的像素按比例放大,所以它们永远不会平滑,只会更加凹凸不平。
参考:http://doc.qt.io/qt-5.6/highdpi.html#high-dpi-support-in-qt
"This will not change the size of point sized fonts"
您需要使用 QT_SCALE_FACTOR 来重新缩放您的应用,而不仅仅是重新缩放其像素。
正如 Qt 文档所说:
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.
你可以尝试做 Qt Creator 正在做的事情:
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);
}
最重要的是最后一行QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
.
这是对我有用的东西。您可以通过在 QApplication 的实例化中提供命令行选项来手动设置 DPIawareness。
官方文档在这里https://doc.qt.io/qt-5/highdpi.html(DPI 意识部分)。
根据文档,您可以将应用程序设置为 DPI Unaware(因此它会自动缩放但显示会模糊),或者设置为 System DPI Aware 或 Per-Monitor Aware。
这是 QApplication 强制高 DPI 实例化的最小示例代码,选择 1 以外的其他值(0 或 2)以启用 DPIUnaware 或 Per Monitor DPI Aware:
int main()
{
int argc = 3;
char*argv[] = {(char*)"Appname", (char*)"--platform", (char*)"windows:dpiawareness=1";
(void) new QApplication(argc, argv);
}
我正在 4k 显示器上开发,这很痛苦...
最后我设法设置了 QtDesigner,然后遇到了这个问题:
当您使用 QT_AUTO_SCREEN_SCALE_FACTOR=1
并使用单选按钮和其他基本小部件编译应用程序时,它在 4k 屏幕上看起来是按比例缩放的。
否则控件的尺寸是正确的,正如预期的那样,它只是不清晰,而是像素化。
我 运行 在 Windows 10 Home 64bit 4k 屏幕上使用 200% DPI 缩放,使用 Qt 5.6 RC msvc2015 64bit 并尝试使用
获得相同的结果QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
如果我用
QGuiApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
控件清晰,文字大小还可以,但所有尺寸都小得多。
如何在高 DPI 屏幕上使控件更清晰?
使用QT_AUTO_SCREEN_SCALE_FACTOR,字体的磅值没有改变,它们只是从原来的像素按比例放大,所以它们永远不会平滑,只会更加凹凸不平。
参考:http://doc.qt.io/qt-5.6/highdpi.html#high-dpi-support-in-qt "This will not change the size of point sized fonts"
您需要使用 QT_SCALE_FACTOR 来重新缩放您的应用,而不仅仅是重新缩放其像素。
正如 Qt 文档所说:
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.
你可以尝试做 Qt Creator 正在做的事情:
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);
}
最重要的是最后一行QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
.
这是对我有用的东西。您可以通过在 QApplication 的实例化中提供命令行选项来手动设置 DPIawareness。
官方文档在这里https://doc.qt.io/qt-5/highdpi.html(DPI 意识部分)。
根据文档,您可以将应用程序设置为 DPI Unaware(因此它会自动缩放但显示会模糊),或者设置为 System DPI Aware 或 Per-Monitor Aware。
这是 QApplication 强制高 DPI 实例化的最小示例代码,选择 1 以外的其他值(0 或 2)以启用 DPIUnaware 或 Per Monitor DPI Aware:
int main()
{
int argc = 3;
char*argv[] = {(char*)"Appname", (char*)"--platform", (char*)"windows:dpiawareness=1";
(void) new QApplication(argc, argv);
}