简体   繁体   中英

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 ?

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 (eg disabled icons that are generated at runtime).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM