views:

806

answers:

3

I'm writing a basic program in python using the PyQt4 module. I'd like to be able to use my system theme's icons for things like the preference dialog's icon, but i have no idea how to do this. So my question is, how do you get the location of an icon, but make sure it changes with the system's icon theme? If it matters, i'm developing this under ubuntu 9.04, so i am using the gnome desktop.

A: 

Use the PyKDE4 KIcon class:

http://api.kde.org/pykde-4.2-api/kdeui/KIcon.html

Thanks for the answer :) Although i would like something that was less dependent on KDE, i'm going to accept this as the answer until something better comes around.
Cdsboy
A: 

I spent a decent amount of researching this myself not long ago, and my conclusion was that, unfortunately, Qt doesn't provide this functionality in a cross-platform fashion. Ideally the QIcon class would have defaults for file open, save, '+', '-', preferences, etc, but considering it doesn't you'll have to grab the appropriate icon for your desktop environment.

Daniel
I've come to that same conclusion. I'm going to write up an answer that includes methods for both KDE and Gnome.
Cdsboy
+5  A: 

Unfortunately, It appears that Qt does not support getting icons for a specific theme. There are ways to do this for both KDE and Gnome.

The KDE way is quite elegant, which makes sense considering that Qt is KDE's toolkit. Instead of using the PyQt4.QtGui class QIcon, you instead use the PyKDE4.kdeui class KIcon. An example of this is:

from PyKDE4.kdeui import *
icon = KIcon("*The Icon Name*")

see the PyKDE documentation for this class, here.

One way to gain support for this for gnome is to use the python gtk package. It is not as nice as the kde way, but it works none the less. It can be used like this:

from PyQt4 import QtGui
from gtk import icon_theme_get_default

iconTheme = icon_theme_get_default()
iconInfo = iconTheme.lookup_icon("*The Icon Name*", *Int of the icon size*, 0)
icon = QtGui.QIcon(iconInfo.get_filename())

See the documentation for the Icon Theme class and Icon Info class.

EDIT: thanks for the correction CesarB

Cdsboy
Qt is KDE's toolkit, not window manager.
CesarB