QIcon: fall back to QPA engine if themes don't provide the icon

If the QIconLoaderEngine (which respects theme and fallback theme) does
not provide the icon, then try the QIconEngine provided by the
QPlatformTheme implementation of createIconEngine. If that provides the
requested icon, then use it; otherwise keep using an QIconLoaderEngine.

Clean up the logic of that code a bit, while maintaining the invariant
that we need to return a valid QIconEngine pointer.

[ChangeLog][QtGui][QIcon] If neither theme nor fallback theme provide
the requested icon, we fall back to the QPA plugin implementation. On
platforms where an implementation exists, this will override the
explicitly provided fallback (second parameter to QIcon::fromTheme).

Change-Id: Idbf9ed049f5e975e1fbcdbb30dbdf6eac08e5827
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
bb10
Volker Hilsheimer 2023-12-06 11:16:13 +01:00
parent 2ed64adb9f
commit 8988c92b98
1 changed files with 12 additions and 4 deletions

View File

@ -626,12 +626,20 @@ QIconEngine *QIconLoader::iconEngine(const QString &iconName) const
{
qCDebug(lcIconLoader) << "Resolving icon engine for icon" << iconName;
auto *platformTheme = QGuiApplicationPrivate::platformTheme();
std::unique_ptr<QIconEngine> iconEngine;
if (!hasUserTheme() && platformTheme)
iconEngine.reset(platformTheme->createIconEngine(iconName));
if (!iconEngine || iconEngine->isNull()) {
if (hasUserTheme())
iconEngine.reset(new QIconLoaderEngine(iconName));
if (!iconEngine || iconEngine->isNull()) {
qCDebug(lcIconLoader) << "Icon is not available from theme or fallback theme.";
if (auto *platformTheme = QGuiApplicationPrivate::platformTheme()) {
qCDebug(lcIconLoader) << "Trying platform engine.";
iconEngine.reset(platformTheme->createIconEngine(iconName));
}
// We need to maintain the invariant that the QIcon has a valid engine
if (!iconEngine || iconEngine->isNull())
iconEngine.reset(new QIconLoaderEngine(iconName));
else
qCDebug(lcIconLoader) << "Icon provided by platform engine.";
}
qCDebug(lcIconLoader) << "Resulting engine" << iconEngine.get();