高 DPI (每英寸點數) 顯示增加瞭像素密度,相較標準 DPI 顯示。
Pixel density is measured in Dots per Inch (DPI) or Pixels per Inch (PPI), and is determined by the number of display pixels and their size. Consequently, the number of pixels alone isn't enough to determine if a display falls into the high-DPI category.
A 4K monitor has a fixed number of pixels (~8M), however its DPI varies between 185 (23 inches) and 110 (40 inches). The former is around twice the standard 96 DPI desktop resolution; the latter barely exceeds this resolution.
High DPI displays bring about some challenges for existing applications:
Traditionally, to support high DPI, Qt scales fonts automatically and provides a DPI value that application code can use to scale the rest of the UI.
Qt supports a high DPI mode where the main coordinate system is virtualized and made independent from the display pixel density. Some operating systems, like macOS and iOS implement this mode. Additionally, if an operating system doesn't support this mode, Qt has an implementation to fallback on.
Now, geometry is specified in device independent pixels. This includes widget and item geometry, event geometry, desktop, window and screen geometry, as well as animation velocities. The output is rendered in device pixels, which corresponds to the display resolution. The devicePixelRatio is the ratio between the device independent pixels and the device pixel coordinate system.
Typically, most applications work with device independent pixels; except for OpenGL and code for raster graphics.
支持 Qt 的操作係統為高 DPI (每英寸點數) 顯示提供瞭下列:
The Apple platforms implement scaling and coordinate system virtualization in the operating system. Normally, no special configuration is required.
On macOS, high-DPI support is enabled by settings in the
Info.plist
file; so make sure these settings are present.
<key>NSPrincipalClass</key> <string>NSApplication</string> <key>NSHighResolutionCapable</key> <string>True</string>
新版 qmake 將生成
Info.plist
file with the NSPrincipalClass key; this is sufficient since NSHighResolutionCapable is true by default.
注意: Both macOS and iOS may apply further virtualization, such that device pixels no longer correspond to display pixels 1:1. This happens on the iPhone 6+ and on macOS configured with "display scaling" enabled.
Scaling
Users choose a scaling factor from the 控製麵闆 or via the context menu. This works by making the functions for querying the system metrics return different values for standard font sizes, sizes of window borders, and so on. It doesn't perform any actual scaling.
DPI 感知
An application on Windows can assume one of the following levels of "DPI Awareness":
| DPI 感知級彆 | 含義 |
|---|---|
| DPI 不感知 | This level was introduced in Windows Vista. To the application, Windows pretends as if it's running on a standard display of 96 DPI of 1920x1080 and scales the application accordingly. It's intended to accommodate older applications designed for low DPI displays. This type of scaling may result in some artifacts. |
| 係統 DPI 感知 | This level was introduced in Windows Vista. It differs from 每監視器 DPI 感知 only when multiple monitors are connected. Windows calculates a scaling that's suitable for all monitors connected. |
| 每監視器 DPI 感知 | This level was introduced in Windows 8.1. Windows does not perform any scaling at all. |
默認情況下,Qt 應用程序被設為 每監視器 DPI 感知 在 Windows 8.1 或 係統 DPI 感知 on older Windows versions. As of Qt 5.4, this level can be specified via a parameter to the platform plugin:
<application> -platform windows:dpiawareness=0,1,2
更多信息,見 使用 qt.conf .
Qt provides the following ways for you to handle high DPI support in your application.
QT_AUTO_SCREEN_SCALE_FACTOR
[boolean] enables automatic scaling, based on the monitor's pixel density. This won't change the size of point-sized fonts, since point is a physical measurement unit. Multiple screens may get different scale factors.
QT_SCALE_FACTOR
[numeric] defines a global scale factor for the whole application, including point-sized fonts.
QT_SCREEN_SCALE_FACTORS
[list] specifies scale factors for each screen. This won't change the size of point-sized fonts. The environment variable is mainly useful for debugging, or to work around monitors with wrong
EDID information
(Extended Display Identification Data).
The format can either be a semicolon-separated list of scale factors in the same order as
QGuiApplication::screens
(), or a semicolon-separated list of
name=value
pairs, where
名稱
如同
QScreen::name
().
While the macOS style fully supports high-DPI, the Windows desktop style currently has some limitations with certain scale factors. In these cases, consider using the Fusion style instead, which supports high-DPI in all cases.
注意: Non-integer scale factors may cause significant scaling/painting artifacts.
Qt::AA_EnableHighDpiScaling
application attribute, introduced in Qt 5.6, enables automatic scaling based on the monitor's pixel density.
Qt::AA_DisableHighDpiScaling
application attribute, introduced in Qt 5.6, turns off all scaling. This is intended for applications that require actual window system coordinates, regardless of environment variables. This attribute takes priority over
Qt::AA_EnableHighDpiScaling
.
Round
,
Ceil
,
Floor
,
RoundPreferFloor
,
PassThrough
。見
Qt::HighDpiScaleFactorRoundingPolicy
enum documentation for a full description of the options.
QT_DEVICE_PIXEL_RATIO
environment variable, that you could set to a numerical scale factor or
auto
。該變量在 Qt 5.6 棄用。
To get an application designed for low DPI values running on high resolution monitors quickly, consider one of the following:
QT_AUTO_SCREEN_SCALE_FACTOR
環境變量到
1
.
然而,這些選項可能導緻某些比例縮放 (或描繪僞影)。
從長遠來看,應用程序應適應不加修改地運行:
| 術語 | 定義 |
|---|---|
| Device Independent Pixels | The pixels that an application uses (user space), subject to scaling by the operating system or Qt. |
| Device Pixels | 顯示設備的像素數。 |
| Device Pixel Ratio | The scale factor that either the operating system or Qt applies. |
| Logical DPI | The resolution used to convert font sizes defined in points to font sizes in pixels. The standard values are 96, 128, ... 192. |
| Physical DPI | The physical resolution obtained by dividing the size of the monitor by the number of pixels. |
| User Space | The coordinate space that an application uses in Device Independent Pixels. |