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 <volker.hilsheimer@qt.io>
(cherry picked from commit 9684691d51335362665e85ced591cffae3ea5360)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2024-11-17 16:26:17 +01:00 committed by Qt Cherry-pick Bot
parent 9a8bc72b93
commit 6cdae4ea3e

View File

@ -1188,10 +1188,23 @@ void QIcon::addFile(const QString &fileName, const QSize &size, Mode mode, State
return; return;
// Check if a "@Nx" file exists and add it. // Check if a "@Nx" file exists and add it.
QString atNxFileName = qt_findAtNxFile(fileName, qApp->devicePixelRatio()); QVarLengthArray<int, 4> 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<int>());
qreal sourceDevicePixelRatio = std::numeric_limits<qreal>::max();
for (const auto dpr : std::as_const(devicePixelRatios)) {
if (dpr >= sourceDevicePixelRatio)
continue;
const QString atNxFileName = qt_findAtNxFile(fileName, dpr, &sourceDevicePixelRatio);
if (atNxFileName != fileName) if (atNxFileName != fileName)
d->engine->addFile(atNxFileName, size, mode, state); d->engine->addFile(atNxFileName, size, mode, state);
} }
}
/*! /*!
Returns a list of available icon sizes for the specified \a mode and Returns a list of available icon sizes for the specified \a mode and
@ -2030,6 +2043,8 @@ QDebug operator<<(QDebug dbg, const QIcon &i)
QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio, QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio,
qreal *sourceDevicePixelRatio) qreal *sourceDevicePixelRatio)
{ {
if (sourceDevicePixelRatio)
*sourceDevicePixelRatio = 1;
if (targetDevicePixelRatio <= 1.0) if (targetDevicePixelRatio <= 1.0)
return baseFileName; return baseFileName;