From 6cdae4ea3efedde0fa3bad831ef07aaa12048ac2 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 17 Nov 2024 16:26:17 +0100 Subject: [PATCH] QIcon: call qt_findAtNxFile for all devicePixelRatios available QFile::addFile() was searching for '@N' - image file where N corresponds to the devicePixelRatio of the QGuiApplication. Since we can have more than one screen, all different devicePixelRatios should be considered. Change-Id: I7c844e163c5b5ca8752d8461139d7abf39c2e8a5 Reviewed-by: Volker Hilsheimer (cherry picked from commit 9684691d51335362665e85ced591cffae3ea5360) Reviewed-by: Qt Cherry-pick Bot --- src/gui/image/qicon.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/gui/image/qicon.cpp b/src/gui/image/qicon.cpp index 854e936d6ea..70ddb1f4bc2 100644 --- a/src/gui/image/qicon.cpp +++ b/src/gui/image/qicon.cpp @@ -1188,9 +1188,22 @@ void QIcon::addFile(const QString &fileName, const QSize &size, Mode mode, State return; // Check if a "@Nx" file exists and add it. - QString atNxFileName = qt_findAtNxFile(fileName, qApp->devicePixelRatio()); - if (atNxFileName != fileName) - d->engine->addFile(atNxFileName, size, mode, state); + QVarLengthArray devicePixelRatios; + const auto screens = qApp->screens(); + for (const auto *screen : screens) { + const auto dpr = qCeil(screen->devicePixelRatio()); // qt_findAtNxFile only supports integer values + if (dpr >= 1 && !devicePixelRatios.contains(dpr)) + devicePixelRatios.push_back(dpr); + } + std::sort(devicePixelRatios.begin(), devicePixelRatios.end(), std::greater()); + qreal sourceDevicePixelRatio = std::numeric_limits::max(); + for (const auto dpr : std::as_const(devicePixelRatios)) { + if (dpr >= sourceDevicePixelRatio) + continue; + const QString atNxFileName = qt_findAtNxFile(fileName, dpr, &sourceDevicePixelRatio); + if (atNxFileName != fileName) + d->engine->addFile(atNxFileName, size, mode, state); + } } /*! @@ -2030,6 +2043,8 @@ QDebug operator<<(QDebug dbg, const QIcon &i) QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio, qreal *sourceDevicePixelRatio) { + if (sourceDevicePixelRatio) + *sourceDevicePixelRatio = 1; if (targetDevicePixelRatio <= 1.0) return baseFileName;