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.

Change-Id: Ia8d14062de7c53601fd9dac30f87a9e672aa2207
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit dde45bcefb1626c26de310a95321bb5e8e4c0014)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2023-05-10 18:27:27 +02:00 committed by Qt Cherry-pick Bot
parent a9c29029d3
commit c00d99d96e
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. 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. The default value will depend on the platform.
\sa setFallbackSearchPaths(), themeSearchPaths() \sa setFallbackSearchPaths(), themeSearchPaths()
@ -1170,6 +1175,11 @@ QStringList QIcon::fallbackSearchPaths()
Sets the fallback search paths for icons to \a paths. 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: \note To add some path without replacing existing ones:
\snippet code/src_gui_image_qicon.cpp 5 \snippet code/src_gui_image_qicon.cpp 5

View File

@ -589,10 +589,11 @@ QThemeIconInfo QIconLoader::loadIcon(const QString &name) const
if (!themeName().isEmpty()) { if (!themeName().isEmpty()) {
QStringList visited; QStringList visited;
iconInfo = findIconHelper(themeName(), name, 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; qCDebug(lcIconLoader) << "Resulting icon entries" << iconInfo.entries;
return iconInfo; return iconInfo;
} }

View File

@ -727,6 +727,18 @@ void tst_QIcon::fromTheme()
abIcon = QIcon::fromTheme("address-book-new"); abIcon = QIcon::fromTheme("address-book-new");
QVERIFY(abIcon.isNull()); 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 // Passing a full path to fromTheme is not very useful, but should work anyway
QIcon fullPathIcon = QIcon::fromTheme(m_pngImageFileName); QIcon fullPathIcon = QIcon::fromTheme(m_pngImageFileName);
QVERIFY(!fullPathIcon.isNull()); QVERIFY(!fullPathIcon.isNull());