Qt already provides a set of standard icons. The nice thing about these icons is that they adapt to the current desktop environment. However the current selection of available icons is rather limited, at least compared to what KDE apps have at their disposal through the freedesktop icon specification. Since most of these icons only are available on the X11 platform, we still haven’t decided how to make them available to pure Qt applications such as Designer and Creator. While we are thinking about that, I thought I might as well wrap up the essential code and provide it as a simple convenience class, QtIconLoader, that you can use in your own project. You can find the source code here.
The class currently only provides one static function:
QIcon QtIconLoader::icon(const QString &iconName, const QIcon &fallback = QIcon()).
Usage should be fairly obvious:
new QAction(QtIconLoader::icon(“document-new”), tr(“&New”), this);
The fallback is of course for platform compatibility so you can provide your own masterpiece as a fallback for other platforms, which is probably the icon you are already using.
And since no blog post is complete without some eye candy, I modified our well know textedit example to QtIconLoader:
And since we also pick up GNOME settings, this is how it looks when running GNOME:
I’m sure there are bugs. For some reason freedesktop standardized on everything except a convenient way to detect which theme to use, so we need to do some trickery to get to the KDE and GNOME settings. Feel free to report issues and suggestions here.