From 1c0a56a2f3300750978f2d6c703b2b917e03e718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20S=C3=B8rvig?= Date: Fri, 20 May 2022 11:19:20 +0200 Subject: [PATCH] Apply ScaleFactorRoundingPolicy to QT_SCREEN_SCALE_FACTORS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QT_SCREEN_SCALE_FACTORS in many cases set on behalf of the user, instead of by the user, so we should make it less sharp and more in line with standard high-dpi configuration. Specifically, make it subject to the rounding policy set by QGuiApplication::setHighDpiScaleFactorRoundingPolicy(). This means that applications which support integer scale factors only will see integers only, also when QT_SCREEN_SCALE_FACTORS specifies a fractional factor. Users who want to override can set QT_SCALE_FACTOR_ROUNDING_POLICY=PassThrough to restore the default Qt rounding behavior. [ChangeLog][QtGui] The high-DPI scale factor rounding policy (settable with QGuiApplication::setHighDpiScaleFactorRoundingPolicy() or QT_SCALE_FACTOR_ROUNDING_POLICY) now applies to scale factors set with QT_SCREEN_SCALE_FACTORS. Pick-to: 6.4 Fixes: QTBUG-95930 Fixes: QTBUG-99546 Change-Id: Ibb0aa5cb9d3a356d33429d0efe69d984b2530728 Reviewed-by: Tor Arne Vestbø --- src/gui/kernel/qhighdpiscaling.cpp | 4 +++- tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/gui/kernel/qhighdpiscaling.cpp b/src/gui/kernel/qhighdpiscaling.cpp index 2f759158ee..6c34af1f9f 100644 --- a/src/gui/kernel/qhighdpiscaling.cpp +++ b/src/gui/kernel/qhighdpiscaling.cpp @@ -540,8 +540,10 @@ static const char scaleFactorProperty[] = "_q_scaleFactor"; /* Sets a per-screen scale factor. */ -void QHighDpiScaling::setScreenFactor(QScreen *screen, qreal factor) +void QHighDpiScaling::setScreenFactor(QScreen *screen, qreal rawFactor) { + qreal factor = roundScaleFactor(rawFactor); + if (!qFuzzyCompare(factor, qreal(1))) { m_screenFactorSet = true; m_active = true; diff --git a/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp b/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp index f221c9db90..62f61e43fa 100644 --- a/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp +++ b/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp @@ -313,9 +313,10 @@ void tst_QHighDpi::environment_QT_SCREEN_SCALE_FACTORS() QFETCH(QByteArray, environment); QFETCH(QList, expectedDprValues); + qputenv("QT_SCREEN_SCALE_FACTORS", environment); + // Verify that setting QT_SCREEN_SCALE_FACTORS overrides the from-platform-screen-DPI DPR. { - qputenv("QT_SCREEN_SCALE_FACTORS", environment); std::unique_ptr app(createStandardOffscreenApp(platformScreenDpi)); int i = 0; for (QScreen *screen : app->screens()) { @@ -327,6 +328,18 @@ void tst_QHighDpi::environment_QT_SCREEN_SCALE_FACTORS() QCOMPARE(window.devicePixelRatio(), expextedDpr); } } + + // Verify that setHighDpiScaleFactorRoundingPolicy applies to QT_SCREEN_SCALE_FACTORS as well + QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round); + { + std::unique_ptr app(createStandardOffscreenApp(platformScreenDpi)); + int i = 0; + for (QScreen *screen : app->screens()) { + qreal expectedRounderDpr = qRound(expectedDprValues[i++]); + qreal windowDpr = QWindow(screen).devicePixelRatio(); + QCOMPARE(windowDpr, expectedRounderDpr); + } + } } void tst_QHighDpi::environment_QT_USE_PHYSICAL_DPI()