Consult QIcon::fallbackSearchPaths() even when theme name is empty

The use of fallback icons should not depend on a theme being set.

[ChangeLog][QtGui][QIcon] QIcon::fallbackSearchPaths() will now be consulted
for fallback icons even if the current theme name is empty.

Pick-to: 6.5 6.6
Change-Id: Ia8d14062de7c53601fd9dac30f87a9e672aa2207
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Tor Arne Vestbø 2023-05-10 18:27:27 +02:00
parent bb16c215e3
commit dde45bcefb
3 changed files with 25 additions and 2 deletions

View File

@ -1156,6 +1156,11 @@ QStringList QIcon::themeSearchPaths()
Returns the fallback search paths for icons.
The fallback search paths are used to look for standalone
icon files if the \l{themeName()}{current icon theme}
or \l{fallbackIconTheme()}{fallback icon theme} do
not provide results for an icon lookup.
The default value will depend on the platform.
\sa setFallbackSearchPaths(), themeSearchPaths()
@ -1170,6 +1175,11 @@ QStringList QIcon::fallbackSearchPaths()
Sets the fallback search paths for icons to \a paths.
The fallback search paths are used to look for standalone
icon files if the \l{themeName()}{current icon theme}
or \l{fallbackIconTheme()}{fallback icon theme} do
not provide results for an icon lookup.
\note To add some path without replacing existing ones:
\snippet code/src_gui_image_qicon.cpp 5

View File

@ -585,10 +585,11 @@ QThemeIconInfo QIconLoader::loadIcon(const QString &name) const
if (!themeName().isEmpty()) {
QStringList visited;
iconInfo = findIconHelper(themeName(), name, visited);
if (iconInfo.entries.empty())
iconInfo = lookupFallbackIcon(name);
}
if (iconInfo.entries.empty())
iconInfo = lookupFallbackIcon(name);
qCDebug(lcIconLoader) << "Resulting icon entries" << iconInfo.entries;
return iconInfo;
}

View File

@ -727,6 +727,18 @@ void tst_QIcon::fromTheme()
abIcon = QIcon::fromTheme("address-book-new");
QVERIFY(abIcon.isNull());
// Test fallback icon behavior for empty theme names.
// Can only reliably test this on systems that don't have a
// named system icon theme.
QIcon::setThemeName(""); // Reset user-theme
if (QIcon::themeName().isEmpty()) {
// Test icon from fallback path even when theme name is empty
fallbackIcon = QIcon::fromTheme("red");
QVERIFY(!fallbackIcon.isNull());
QVERIFY(QIcon::hasThemeIcon("red"));
QCOMPARE(fallbackIcon.availableSizes().size(), 1);
}
// Passing a full path to fromTheme is not very useful, but should work anyway
QIcon fullPathIcon = QIcon::fromTheme(m_pngImageFileName);
QVERIFY(!fullPathIcon.isNull());