Freedesktop icons in Qt

Posted by Jens Bache-Wiig on February 13, 2009 · 10 comments

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:

Here is how it looks running on KDE 4.2:
QTextEdit in KDE

And since we also pick up GNOME settings, this is how it looks when running GNOME:

QTextEdit in 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.

QShare(this)

No related posts.


10 comments

1 Carlos Ortega February 14, 2009 at 12:28 am
 

We want news from a O.S that >90% world use !
We want news of Windows !
Change, we can !

2 Stephan Sokolow February 14, 2009 at 4:11 am
 

As I remember, Windows doesn’t have an icon theming mechanism. In fact, one Linux geek who was trying Windows for the first time in half a decade described it as “On Linux, your desktop is the appliance. On Windows, each application is an appliance, complete with its own visual style.” (He wasn’t trying to criticize, but I still see that as a downside)

3 Jens February 14, 2009 at 9:23 am
 

Stephan: Windows do ship with a few libraries containing common icons for files and folders but haven’t made any attempt on standardizing common icons such as cut and edit. We already expose many of those in QStyle::standardIcon but already have to make our own placeholders for icons that does not exist on Windows. On Mac the situation is somewhat similar. To support the freedesktop icon spec on Windows and Mac we would have to bundle a complete icon theme inside QtGui. Perhaps the best way to do this cross-platform is to simply expand on the set of standard icons we have though

4 alexis.menard February 14, 2009 at 6:23 pm
 

Carlos Ortega :

http://labs.trolltech.com/blogs/2009/02/13/bnilsenflush/
Apply on all platforms.

http://labs.trolltech.com/blogs/2009/02/10/why-kde-42-should-use-qt-45/
Even if it’s related to KDE, it globally says that all stuff related to QGraphicsView are better, performance improved and bugs fixed. More than 50% of our bugs are cross platform, we fix one platform, we fix others.

http://labs.trolltech.com/blogs/2009/02/08/qt-kinetic-hits-plasma/
Qt Kinetic, Fully cross platform when it will be release. Since plasma is the only big real test case (with source code), we use it to test things…

http://labs.trolltech.com/blogs/2009/02/06/improvements-to-qgraphicsitemcachemodes-insides/
Apply on all platforms.

And http://labs.trolltech.com/blogs/2009/02/05/the-new-and-improved-qt-creator-092-rc/
All platforms.

So please read more further than just the title or if there is KDE/Linux in the blog post. We improve ALL platforms every day as much as possible. Some work are platform dependant like this one…it can happen…

5 Ryan Cuthbertson February 16, 2009 at 12:05 am
 

“You can find the source code here.”
But I can’t. The project seems to be empty. Eg.
$ svn checkout http://qticonloader.googlecode.com/svn/trunk/qticonloader-read-only
svn: URL ‘http://qticonloader.googlecode.com/svn/trunk/qticonloader-read-only‘ doesn’t exist

Is this code available under the commercial licence?

6 aaron.jamis February 16, 2009 at 12:32 am
 

Yatak ( Yes, another topic about KDE)

7 Richard February 16, 2009 at 11:46 am
 

I would be great if Qt supported a greater set of standard icons ‘out of the box’ for all platforms.

When quickly prototyping an application, it is a pain to have to design or collect various icons for standard actions such as open, save, cut, copy, paste, print, undo, redo, and so on. A good start would be to refactor the existing Qt code to make all the icons already currently used in Qt such as those from Designer, Assistant, QPrintPreviewDialog, etc immediately available to coders via QStyle::standardIcon or similar.

8 Michael Hofmann February 16, 2009 at 5:20 pm
 

Thanks for the cool project. Could you relicense it under GPL 2 or 3/GPL 2 or later so it can be used in GPL 3 projects?

@Ryan: > Source is available trough svn: svn://labs.trolltech.com/svn/iconloader

9 Jens February 16, 2009 at 6:53 pm
 

@Richard : I agree, while doing the example above I feel like all those icons belong as part of the core API. That might be better than exposing this functionality directly and sufficient for most people.
@Michael: Thanks for that! It is already be available as GPL 2 and I’m certainly relicensing it under the LGPL once 4.5 has been released.

10 Jens February 16, 2009 at 7:00 pm
 

@Michael: Sorry, I was a bit quick to reply ignoring the “and later” part. ;) I’ll see if I can change the license. Just used the most restrictive one for now until I have some sort of approval.
Edit : I changed it to use the Qt license which is GPL 2+3 and commercial.

Comments on this entry are closed.

Previous post:

Next post: