Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System theme icons and PyQt4

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.

like image 389
Cdsboy Avatar asked Jun 15 '09 19:06

Cdsboy


1 Answers

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

like image 71
Cdsboy Avatar answered Oct 16 '22 16:10

Cdsboy