RichText: how to insert "icon from theme" ?
-
Hi all,
I'd like to build a QLabel which displays rich text and I'd like to insert an icon from the theme spec. I know I can insert images from a resource file but how about icons from themes ?
Thanks -
You mention QLabel, which I recognize is not QML and yet I am about to share something that worked for me in QML.
So please take my answer as a "hint." All my Qt work is in QML (not QWidgets), so my experience does not overlap directly with your use case.
In Qt5, I was able to utilize an icon that is compiled into one of the binary libraries that is part of the Qt framework.
My line of QML looks like:
source: "qrc:/qt-project.org/imports/QtQuick/Controls.2/images/double-arrow.png"And the "double-arrow.png" is compiled into:
/usr/lib/x86_64-linux-gnu/qt5/qml/QtQuick/Controls.2/libqtquickcontrols2plugin.soI have forgotten how I discovered that "double-arrow" was available to me in this way. I probably encountered it via some sample code or official Qt example app.
If I were to go hunting and digging for options available to me nowadays (in Qt6), I might start (on Linux) with something like this:
strings ./lib/libQt6Gui.so | grep -w png... which shows:
:/qt-project.org/styles/commonstyle/images/file-16.pngwhich tells me that I can probably use "qrc:/qt-project.org/styles/commonstyle/images/file-16.png" just fine.
Similar tactics would probably work for all the PNG files seen here: https://github.com/qt/qtbase/tree/dev/src/widgets/styles/images
-
I guess this would work in a QLabel too because you use an image (not an icon) and its source is inside a file (real or resource).
What I'm trying to achieve is to put an icon from the "system" theme. That's another story I think because it needs resolution.
In fact I would like this function
https://doc.qt.io/qt-6/qicon.html#fromTheme
to be set as a source. -
Hopefully someone with more experience than I with "icon themes" can come and comment. Please, come one; come all 🙏
@dextermagnific Ok got it. Well in your case, I went and skimmed this file: https://github.com/qt/qtbase/blob/dev/src/gui/image/qiconloader.cpp
In skimming the code, it appeared to me that the only platform-specific implementation is
QIconCacheGtkReader.Thankfully (for my sanity), it appears that the docs confirm this:
- https://doc.qt.io/qt-6/qicon.html#themeName (quote: "Platform icon themes are only implemented on Freedesktop based systems at the moment")
- https://doc.qt.io/qt-6/qicon.html#fallbackThemeName (quote: "Platform fallback icon themes are only implemented on Freedesktop based systems at the moment")
So that makes me wonder if you only need your planned icon-theme approach to work on Linux? Have you tried QIcon::fromTheme on Windows or Mac and were you happy with the results?
(Keep in mind that I am still speaking as someone who has not used the "icon theme" feature of QWidgets even once. Please double-check my comments with added research because I don't want to lead anyone astray!)
If you only need it to work on Linux, then you can probably just look at
QIconCacheGtkReaderand learn how the icon selection is done for Freedesktop. (note to readers: I acknowledge my sloppy language conflating Linux and Freedesktop.)