wa4557
wa4557

Reputation: 1045

PyQt5 QIcon.hasThemeIcon(...) always true

Hello I'm trying to get icons from my current theme on Linux machine (Ubuntu 14.10). As far as I understand this should give me the correct icon:

from PyQt5 import QtGui
icon = QtGui.QIcon.fromTheme('edit-copy')

However

icon.themeName() #and
icon.availableSizes()

return empty strings. Moreover this:

QtGui.QIcon.hasThemeIcon('icon_that_does_definitely_not_exist')

returns True as well. Why?

Moreover: Is there a way to extract the full icon path from my variable icon?

Upvotes: 0

Views: 757

Answers (1)

ekhumoro
ekhumoro

Reputation: 120738

You should always create a QApplication before doing anything involving pixmaps. Your example code doesn't get to the point of trying to create one, but if it did, it would most likely crash immediately. The behaviour of the QIcon methods would probably be best described as "undefined" before a QApplication is created.

But here is what I get when doing things the right way (on Linux):

>>> from PyQt5 import QtGui, QtWidgets
>>> app = QtWidgets.QApplication([''])
>>> icon = QtGui.QIcon.fromTheme('edit-copy')
>>> icon.themeName()
'oxygen'
>>> icon.availableSizes()
[PyQt5.QtCore.QSize(48, 48), PyQt5.QtCore.QSize(32, 32), PyQt5.QtCore.QSize(22, 22), PyQt5.QtCore.QSize(16, 16)]
>>> QtGui.QIcon.hasThemeIcon('icon_that_does_definitely_not_exist')
False

To find out where the icon may have come from, you can try this:

>>> QtGui.QIcon.themeSearchPaths()
['/home/foo/.icons', '/usr/local/share/icons', '/usr/share/icons', ':/icons']

Of course, it makes no sense ask for "the" icon path, because a QIcon represents a group of related images, some of which don't even have a corresponding file on disk (e.g. disabled icons that are generated at runtime).

Upvotes: 1

Related Questions