From 54c2c9ac4e897a5a1ebbdbfa15d94fc452b72ac5 Mon Sep 17 00:00:00 2001 From: Jocelyn Turcotte Date: Mon, 11 Jan 2016 15:27:13 +0100 Subject: [PATCH] Windows: Fix HiDPI #3414 Use QT_DEVICE_PIXEL_RATIO=auto on Qt<=5.5 to enable automatic scale factor settings on Windows. Also move the existing Qt::AA_EnableHighDpiScaling logic to use the equivalent QT_AUTO_SCREEN_SCALE_FACTOR=1 environment variable just to keep the 5.5 and >=5.6 code at the same place. --- src/gui/application.cpp | 6 +----- src/gui/main.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/gui/application.cpp b/src/gui/application.cpp index 301856e89..af1764b8e 100644 --- a/src/gui/application.cpp +++ b/src/gui/application.cpp @@ -113,11 +113,7 @@ Application::Application(int &argc, char **argv) : #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0) setAttribute(Qt::AA_UseHighDpiPixmaps, true); #endif -#if QT_VERSION > QT_VERSION_CHECK(5, 6, 0) - // this may slightly break some styles until they - // get fixed, but will make dialogs readable. - setAttribute(Qt::AA_EnableHighDpiScaling, true); -#endif + parseOptions(arguments()); //no need to waste time; if ( _helpOnly || _versionOnly ) return; diff --git a/src/gui/main.cpp b/src/gui/main.cpp index 83c38063f..28a6858f6 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -47,11 +47,34 @@ int main(int argc, char **argv) { Q_INIT_RESOURCE(client); +#ifdef Q_OS_WIN + // If the font size ratio is set on Windows, we need to + // enable the auto pixelRatio in Qt since we don't + // want to use sizes relative to the font size everywhere. + // This is automatic on OS X, but opt-in on Windows + // https://doc-snapshots.qt.io/qt5-5.6/highdpi.html#qt-support +#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0) + qputenv("QT_DEVICE_PIXEL_RATIO", "auto"); +#else + qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1"); +#endif +#endif // Q_OS_WIN + #ifdef Q_OS_MAC Mac::CocoaInitializer cocoaInit; // RIIA #endif OCC::Application app(argc, argv); +#ifdef Q_OS_WIN + // The Windows style still has pixelated elements with Qt 5.6, + // it's recommended to use the Fusion style in this case, even + // though it looks slightly less native. Check here after the + // QApplication was constructed, but before any QWidget is + // constructed. + if (app.devicePixelRatio() > 1) + QApplication::setStyle(QStringLiteral("fusion")); +#endif // Q_OS_WIN + #ifndef Q_OS_WIN signal(SIGPIPE, SIG_IGN); #endif -- 2.30.2