Search code examples
pythonpyqt5gtk3pygtk

How can I convert a (Py)Gtk Gio.ThemedIcon to anything usable in (Py)Qt5 (QImage, QIcon, etc)?


I have this code that gets a list of all the programs in my linux system capable of opening a png file:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio

app_list = Gio.app_info_get_all_for_type('image/png')

then I can get the program's icon by:

app = app_list[0]
g_icon = app.get_icon()

This returns <Gio.ThemedIcon object at 0x7f2681640d80 (GThemedIcon at 0x25500a0)>.
How can I convert this Gio.ThemedIcon object to anything (pixbuf, bytes, etc) that I can then use to create a QIcon or a QImage?


Solution

  • The ThemedIcon has the get_names() function that returns a list of possible theme names. You can use the static fromTheme() to get the icon in Qt:

    app = app_list[0]
    g_icon = app.get_icon()
    for name in g_icon.get_names():
        icon = QtGui.QIcon.fromTheme(name)
        if not icon.isNull():
            break