The windowing system provides Qt with a per-display logical DPI value.
For Windows, the possible values are:
|DPI percentage||Registry Data||Value|
[ Physical DPI should not be used, since display data is unreliable and does not respect user settings ]
The 100% DPI is the base DPI. The ratio between the actual DPI and the base DPI is the scale factor, aka devicePixelRatio.
By default Qt reports the logical DPI via accessors such as QScreen::logicalDpi(), which applications can use to scale the UI. Fonts are automatically scaled. Historically the problem with this has been that not all UI elements respect the DPI.
In addition Qt supports a mode (AA_EnableHighDpiScaling) where the entire UI is automatically scaled. The logicalDpi accessors then returns the base DPI in order to avoid duplicate scaling. The devicePixelRatio() accessors returns the UI scale factor, but unlike the “classic” mode these are much less used since the scaling is automatic.
Example: For 200% (192 DPI) scaling on Windows we can configure Qt in one of two modes:
1) 192@1x (default)
2) 96@2x (AA_EnableHighDpiScaling)
So far so good.
Fractional scale factors pose a challenge since Qt may not render correctly. One option is then to round the scale factor. When rounding we also have the option of adjusting the logical DPI (as reported to the app) to compensate for the rounding error.
Rounding: We may round down or round up. The former gives visually small UI elements while the latter gives visually large UI elements. Qt currently rounds using qRound(), which for critical values like 250% means ‘up’. We should consider rounding down instead for .5.
DPI: We can fix the DPI to be the base DPI or compensate for the inaccurate scale factor. The former makes the text size match the rest of the application UI, while the latter gives correct text size for the display. We should consider at least not rounding down here (don't present DPI values below the base DPI).
Combined table of options for handling 250%: (AA_EnableHighDpiScaling mode only)
1) firstname.lastname@example.org (correct UI and text size, maybe painting artifacts)
2) 120@2x (small UI with larger text)
3) 96@2x (small UI with matching text)
4) 80@3x (large UI with smaller text) (5.6 behavor)
5) 96@3x (large UI with matching text)
- the platform screen provides the logical dpi
- the platform screen provides the scale factor: qRound(logical-dpi / base-dpi)
- qhighdpiscaling recovers the base-dpi by computing logical-dpi / scale factor.
- qhighdpiscaling provides QScreen::logicalDpi(), which the base dpi for the main screen
- (the platform screen continues to provide the logical dpi)
- the platform screen provides the base-dpi instead of the scale factor
- qhighdpiscaling computes the scale factor
- qhighdpiscaling provides per-screen DPI for QScreen::logicalDpi().
- round .5 scale factors down
- perhaps compensate for rounding by increasing the reported logical DPI.
Control how the scale factor is computed:
QT_SCALE_FACTOR_ROUNDING_POLICY: Round, Ceil, Floor, RoundPreferFloor, PassThrough
Control DPI adjustment:
QT_DPI_ADJUSTMENT_POLICY: AdjustDpi, DontAdjustDpi, AdjustUpOnly
tests/manual/highdpi has been updated with "--metrics" tests that displays the DPI and devicePixelRatio values.
Windows: Windows already uses the logical DPI and can be trivially ported over from pixelDensity() to logicalBaseDpi().
X11: Use logical DPI instead of physical DPI. QT_FONT_DPI is now respected:
Can we possibly use config values from the desktop?
Android The Android base DPI is 160, but QAndroidPlatformScreen::logicalDpi() computes the logical DPI values as density * 72. This might be throwing our scale factor calculations off. Implementing QAndroidPlatformScreen::logicalBaseDpi() and making it return 72 should correct this.
|For Gerrit Dashboard: QTBUG-53022|
|157173,21||Compute logical DPI on a per-screen basis||5.14||qt/qtbase||Status: MERGED||+2||0|
|157174,31||Update Dpi and scale factor computation||5.14||qt/qtbase||Status: MERGED||+2||0|
|157175,33||HighDPI: Add “metrics” manual test||5.14||qt/qtbase||Status: MERGED||+2||0|
|161334,24||Move QT_FONT_DPI to cross-platform code||5.14||qt/qtbase||Status: MERGED||+2||0|
|163219,21||Update QT_SCREEN_SCALE_FACTORS||5.14||qt/qtbase||Status: MERGED||+2||0|
|176381,16||Deprecate QT_AUTO_SCREEN_SCALE_FACTOR||5.14||qt/qtbase||Status: MERGED||+2||0|
|183408,1||Determine per-screen logical DPI||5.8||qt/qtbase||Status: ABANDONED||0||0|
|183409,1||Update Dpi and scale factor computation||5.8||qt/qtbase||Status: ABANDONED||0||0|
|209886,13||Add high-DPI scale factor rounding policy C++ API||5.14||qt/qtbase||Status: MERGED||+2||0|
|222052,4||xcb: round down the scale factor for values < 0.8||5.11||qt/qtbase||Status: MERGED||+2||0|
|231719,6||xcb: get back to normal rounding of the scale factor for big screens||5.11.1||qt/qtbase||Status: ABANDONED||0||0|
|240429,1||xcb: get back to normal rounding of the scale factor for big screens||dev||qt/qtbase||Status: ABANDONED||+1||0|
|240491,3||xcb: use 128 as a reference DPI for small screens||5.12||qt/qtbase||Status: MERGED||+2||0|
|254473,7||WIP: Squashed High DPI scaling calculation||dev||qt/qtbase||Status: ABANDONED||-2||0|
|258305,13||Add screen() accessor to QWidget||5.14||qt/qtbase||Status: MERGED||+2||0|
|272564,3||QtGui: Refactor parsing of the High DPI scaling env variables||5.14||qt/qtbase||Status: MERGED||+2||0|
|279058,5||QHighDpiScaling: fix potential null pointer dereference||5.14||qt/qtbase||Status: MERGED||+2||0|
|280571,2||QHighDpiScaling: impove readability of screenSubfactor method||5.14||qt/qtbase||Status: MERGED||+2||0|