[Qt-interest] QIcon::fromTheme() under Windows

Tanguy Krotoff tkrotoff at gmail.com
Wed Feb 3 14:54:26 CET 2010


I've found the solution by looking at QIcon unit tests:
http://qt.gitorious.com/qt/qt/trees/master/tests/auto/qicon

Here is an example of a valid .qrc file to use with QIcon::fromTheme():
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="icons/silk">
	<file alias="index.theme">icons/silk/index.theme</file>
	<file alias="16x16/actions/application-exit.png">icons/silk/stop.png</file>
	<file alias="16x16/actions/document-open.png">icons/silk/folder_page.png</file>
	[...]

You can also remove the prefix and write it like this:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
	<file alias="icons/silk/index.theme">icons/silk/index.theme</file>
	<file alias="icons/silk/16x16/actions/application-exit.png">icons/silk/stop.png</file>
	[...]

Or even like this depending on your icon theme structure:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
	<file>icons/oxygen-icons/index.theme</file>
	<file>icons/oxygen-icons/16x16/actions/application-exit.png</file>
	[...]

Directory "icons" is by default part of QIcon::themeSearchPaths()
http://qt.nokia.com/doc/4.6/qicon.html#themeSearchPaths
"silk" is the icon theme, see QIcon::setThemeName()
http://qt.nokia.com/doc/4.6/qicon.html#setThemeName
"16x16/actions" is a directory referenced by index.theme

Here the source code directory structure:
> ./silk.qrc
> ./icons/silk/index.theme
> ./icons/silk/*.png

You must provide an index.theme file and it should look like this:
[Icon Theme]
Name=Silk
Comment=Silk Theme
Inherits=default
Directories=16x16/actions,16x16/apps [...]

[16x16/actions]
Size=16
[...]


>From freedesktop documentation
http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
Name, Comment and Directories are required. Inherits is not but if you
don't specify it your application will crash (at least under Windows -
Qt 4.6.1) inside qiconloader.cpp because Qt does not accept empty
theme name for your parent theme. So just specify Inherits=default
More documentation how to create an icon theme here:
http://live.gnome.org/GnomeArt/Tutorials/IconThemes (no such tutorial
on KDE website)


Now inside your source code you can write:

static const char * GENERIC_ICON_TO_CHECK = "document-open";
static const char * FALLBACK_ICON_THEME = "silk";
if (!QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK)) {
	//If there is no default working icon theme then we should
	//use an icon theme that we provide via a .qrc file
	//This case happens under Windows and Mac OS X
	//This does not happen under GNOME or KDE
	QIcon::setThemeName(FALLBACK_ICON_THEME);
}
[...]
QIcon::fromTheme("application-exit");

So instead of providing a fallback icon each time you call
QIcon::fromTheme() you just have to provide a fallback icon theme via
a .qrc file once and for all.


On Tue, Feb 2, 2010 at 7:11 PM, Tanguy Krotoff <tkrotoff at gmail.com> wrote:
> Hi all,
>
> I'm having some hard time with QIcon::fromTheme() under Windows (Qt
> 4.6.1) as I couldn't find any example or code snippet.
> Under Linux I have no problem to make it work.
>
> My ultimate goal is to have a .qrc file under Windows that looks like that:
> <qresource prefix="icons">
>     <file alias="application-exit">icons/silk/stop.png</file>
>     <file alias="document-open">icons/silk/folder_page.png</file>
>     <file alias="document-save">icons/silk/disk.png</file>
>     ...
>
> and then inside my code a simple: QIcon::fromTheme("document-open");
>
> From the documentation, :/icons is supposed to be searched by QIcon::fromTheme()
> Is there an example around that uses QIcon::fromTheme() with a .qrc
> file? Is this suppose to work?
>
> Regards,
>
> --
> Tanguy Krotoff <tkrotoff at gmail.com>
> +33 6 68 42 70 24
>



-- 
Tanguy Krotoff <tkrotoff at gmail.com>
+33 6 68 42 70 24




More information about the Qt-interest-old mailing list