QIcon: Fallback behavior if icon not in the curren theme

Maurizio Berti maurizio.berti at gmail.com
Fri Jul 7 18:00:33 BST 2023


Qt already provides something similar in the static functions of QIcon:

- setFallbackSearchPaths()
<https://doc.qt.io/qt-5/qicon.html#setFallbackSearchPaths>
- setFallbackThemeName()
<https://doc.qt.io/qt-5/qicon.html#setFallbackThemeName>

The QStyle StandardPixmap enum is not used for standard QIcons, as that's
not their intended purpose and they are actually just a few (~60), while a
full icon theme normally includes many more: the list of standard icons
from the freedesktop specification (
https://specifications.freedesktop.org/icon-naming-spec/latest/ar01s04.html
) contains almost 300 names.

Unfortunately, there is no absolute guarantee that an icon actually exists
for a given name in the installed themes, even if it's considered standard.
You can use QIcon.hasThemeIcon()
<https://doc.qt.io/qt-5/qicon.html#hasThemeIcon>, but once you know that
the icon doesn't exist, you're done.

Depending on the complexity of the program, you can consider providing your
own icon theme, eventually redistributing the one you like (as long as its
license allows it and is compatible with your project license) and use it
as a fallback, so that it will only be used whenever the installed icon
theme doesn't provide the required icon. The drawback of doing this is that
the icon styles may be inconsistent.

Cheers,
MaurizioB

Il giorno gio 6 lug 2023 alle ore 14:42 <c.buhtz at posteo.jp> ha scritto:

> Hello,
>
> am I right to say loading icons like this is usual and the recommended
> way (focusing on GNU Linux here)?
>
>      QIcon.fromTheme('media-playback-pause')
>
> How does Qt react if an Icon (by its name) is not available in the
> currents system default theme?
>
> Is there a fallback behavior? Maybe using Qt-inbuild icons from QStyle
> or an alternative theme?
>
> If there is such a behavior in Qt itself how can this be activated?
>
> If not I have to deal with it for my own of course.
>
> Kind
> Christian
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20230707/5cc9670c/attachment.htm>


More information about the PyQt mailing list