高 DPI 显示

高 DPI 显示

高 DPI 显示是相较标准 DPI 显示,增加像素密度的显示。

This pixel density is measured in Dots per Inch (DPI) or Pixels Per Inch (PPI), and is determined by the number of display pixels and physical size. This means that the number of pixels alone is not enough to determine if a display falls into the high-DPI category.

A 4K monitor has a fixed number of pixels (~8M), however the DPI varies between 185 (23 inch) and 110 (40 inch). The former is around 2x standard 96 DPI desktop resolution, while the latter is barely over it.

高 DPI 问题

High DPI Displays cause a number of issues for existing applications:

  • Applications using UI designs with fixed coordinates look small. The combination of font size specification in points and other sizes in pixels is particularly problematic since points are independent of the monitor resolution. For example, a frame of 40x20 pixels around the text "hello" using a 12pt font looks correct on low resolution monitors, but the frame will be too small on high DPI monitors, causing the text to be clipped.
  • Applications must adapt to situations where users have multiple displays with varying resolution. For example, they might use a 4K monitor for the document window of an image editor and a low resolution monitor for the tool box.

The traditional approach to supporting high DPI has been one where Qt scaled fonts automatically, and then provided a DPI value that application code could use to scale the rest of the UI.

Qt 高 DPI 支持概述

Qt supports a high DPI mode where the main coordinate system is virtualized and made independent of the display pixel density. This mode is implemented by some operating systems (macOS, iOS). In addition, Qt contains an implementation which may be used where operating system support is missing.

Geometry is now specified in device independent pixels. This includes widget and item geometry, event geometry, desktop, window and screen geometry, and animation velocities. Rendered output is in device pixels, which corresponds to the display resolution. The ratio between the device independent and device pixel coordinate systems is the devicePixelRatio.

Applications mostly work with device independent pixels. Notable exceptions are OpenGL and code that works with raster graphics.

操作系统支持

The operating systems supported by Qt offer the following support for high DPI displays:

macOS 和 iOS

The Apple platforms implement scaling and coordinate system virtualization in the in the operating system. Normally, no special configuration is required.

注意: On macOS, high-DPI support is enabled by settings in the Info.plist file. Make sure they are present.

<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSHighResolutionCapable</key>
<string>True</string>
					

Newer versions of qmake will generate Info.plist's with the NSPrincipalClass key, which is sufficient since NSHighResolutionCapable is true by default.

注意: macOS and iOS may apply further virtualization such that device pixels do not correspond 1:1 to display pixels. This happens on the iPhone 6+ and on macOS configured with 'display scaling' enabled.

微软 Windows

Scaling

The user can choose a scaling factor from the control panel or via 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 does not perform any actual scaling.

DPI 感知

An application on Windows can assume one of the following levels of "DPI Awareness":

DPI 感知级别 含义
DPI 不感知 This level has been introduced in Windows Vista. Windows will pretend to the application that it is running on a standard display of 96 DPI of 1920x1080 and scale the application accordingly. It is intended to accommodate older applications designed for low DPI displays. Some artifacts may result from this type of scaling.
系统 DPI 感知 This level has been introduced in Windows Vista. It differs from 每监视器 DPI 感知 only when multiple monitors are connected. Windows will calculate a scaling suitable for all connected monitors.
每监视器 DPI 感知 This level has been introduced in Windows 8.1. Windows does not perform any scaling at all.

Qt applications by default are 每监视器 DPI 感知 在 Windows 8.1 或 系统 DPI 感知 on older versions of Windows. As of Qt 5.4, the level can be specified by passing a parameter to the platform plugin (see 使用 qt.conf ):

<application> -platform windows:dpiawareness=0,1,2
					
					

Qt 中的高 DPI 支持

  • Ability to provide pixmaps or artwork for high resolution: see 绘制高分辨率版本的像素图和图像 .
  • Qt 5.6 supports cross-platform high-DPI scaling for legacy applications, similar to the scaling done natively by macOS. This allows applications written for low-DPI screens to run unchanged on high-DPI devices. This feature is opt-in, and can be enabled by the following environment variables:
    • QT_AUTO_SCREEN_SCALE_FACTOR [boolean] enables automatic scaling, based on the pixel density of the monitor. This will not change the size of point sized fonts, since point is a physical unit of measure. 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 will not change the size of point sized fonts. This environment variable is mainly useful for debugging, or to work around monitors with wrong EDID information (Extended Display Identification Data).

      The format can be either 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 aims to support high-DPI in all cases.

    注意: Non-integer scale factors may cause significant scaling/painting artifacts.

  • The application attribute Qt::AA_EnableHighDpiScaling , introduced in Qt 5.6, enables automatic scaling based on the pixel density of the monitor.
  • The application attribute Qt::AA_DisableHighDpiScaling , introduced in Qt 5.6, turns off all scaling. This is intended for applications that need to use actual window system coordinates, regardless of environment variables. This attribute takes priority over Qt::AA_EnableHighDpiScaling .
  • An experimental implementation of high-DPI scaling was introduced in Qt 5.4. It was enabled by the environment variable QT_DEVICE_PIXEL_RATIO , which could be set to a numerical scale factor or "auto" . This variable is deprecated in Qt 5.6.

现有应用程序的迁移

In order to get an application designed for low DPI values running on a high resolution monitors quickly, consider one of the scaling options (let the application run as DPI 不感知 on Windows or set the environment variable QT_AUTO_SCREEN_SCALE_FACTOR to "1" . These options may incur some scaling or painting artifacts, though.

In the longer term, the application should be adapted to run unmodified:

  • Always use the qreal versions of the QPainter 绘制 API。
  • Size windows and dialogs in relation to the screen size.
  • Replace hard-coded sizes in layouts and drawing code by values calculated from font metrics or screen size.

高 DPI 术语词汇表

术语 定义
Device Independent Pixels Pixels used by application (user space), subject to scaling by the operating system or Qt.
Device Pixels Pixels of the display device.
Device Pixel Ratio Scale factor applied by the operating system or Qt.
Logical DPI Resolution used for converting font sizes defined in points to font sizes in pixels. Typically one of the standard values 96, 128, .. 192.
Physical DPI Physical resolution obtained by dividing the size of the monitor by the number of pixels.
Retina Display Wikipedia on Retina Displays
User Space The coordinate space the application uses (Device Independent Pixels).