且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在高 dpi Windows 平台上自动重新缩放应用程序?

更新时间:2023-01-26 10:49:34

使用固定坐标和大小的应用程序在高 DPI 分辨率下看起来很小.尽管即使使用布局也存在一些关于元素和字体大小和边距的问题.幸运的是,从 Qt 5.4 开始就支持高 DPI 显示,因为已经有许多 高 DPI 问题修复.

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.

Windows 上的应用程序可以采用以下DPI Awareness"级别之一(来自 Qt 文档):

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

  • DPI Unaware:此级别已在 Windows-Vista 中引入.Windows 会假装应用程序正在运行1920x1080 的 96 DPI 标准显示和缩放应用程序因此.它旨在容纳设计的较旧的应用程序用于低 DPI 显示器.某些工件可能是由这种类型的缩放.
  • 系统-DPI 感知:此级别已在 Windows-Vista 中引入.它与 Per-Monitor DPI Aware 不同,仅当多个显示器连接的.Windows 将计算适合所有连接的缩放比例监视器.
  • 每显示器 DPI 感知:此级别已在 Windows 8.1 中引入.Windows 根本不执行任何缩放.

它还指出:

默认情况下,Qt 应用程序在 Windows 8.1 或旧版 Windows 上的系统 DPI 感知.从 Qt 5.4 开始,级别可以通过将参数传递给平台插件来指定(请参阅使用 qt.conf):

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.

一般来说,要在高 DPI 显示器上拥有良好的用户界面,请考虑以下事项:

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

  • 使用最新版本的 Qt
  • 使用布局并避免固定尺寸(除非您自己计算缩放比例)
  • 根据您的应用程序需要进行适当的 DPI 相关设置,例如,如果您使用 QPainter 和像素图,则设置 Qt::AA_UseHighDpiPixmaps 属性,或计算缩放比例用于在特殊情况下调整某些元素大小.
  • 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.