Do not create icon engine multiple times

Creating an icon engine is a potentially expensive operation involving
many file lookups. In the case where neither QIconLoaderEngine nor the
engine from the platform theme is valid, QIconLoader tries to unsuccesfully
find the icon in all theme directories, the theme engine is constructed
potentially doing some expensive operation and finally a new
QIconLoaderEngine is constructed which does all the file lookups again.
Instead keep the existing QIconLoaderEngine around if it was already
constructed.

Pick-to: 6.7
Change-Id: Iace9a3f904730064f44939b2269316484ac6da2e
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
David Redondo 2023-12-12 15:56:39 +01:00
parent 9ea9e2476d
commit 620373e1ad

View File

@ -633,14 +633,16 @@ QIconEngine *QIconLoader::iconEngine(const QString &iconName) const
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));
std::unique_ptr<QIconEngine> themeEngine(platformTheme->createIconEngine(iconName));
if (themeEngine && !themeEngine->isNull()) {
iconEngine = std::move(themeEngine);
qCDebug(lcIconLoader) << "Icon provided by platform engine.";
}
}
// 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.";
}
// We need to maintain the invariant that the QIcon has a valid engine
if (!iconEngine)
iconEngine.reset(new QIconLoaderEngine(iconName));
qCDebug(lcIconLoader) << "Resulting engine" << iconEngine.get();
return iconEngine.release();