简体   繁体   English

PyQt5 QIcon.hasThemeIcon(…)始终为true

[英]PyQt5 QIcon.hasThemeIcon(…) always true

Hello I'm trying to get icons from my current theme on Linux machine (Ubuntu 14.10). 您好,我正在尝试从Linux计算机(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. 也返回True Why? 为什么?

Moreover: Is there a way to extract the full icon path from my variable icon ? 此外:有没有办法从我的可变icon提取完整的图标路径?

You should always create a QApplication before doing anything involving pixmaps. 在进行涉及像素图的任何操作之前,您应该始终创建一个QApplication 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. 在创建QApplication之前,最好将QIcon方法的行为描述为“未定义”。

But here is what I get when doing things the right way (on Linux): 但是,当我以正确的方式做事时(在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”图标路径是没有意义的,因为QIcon代表一组相关的图像,其中一些甚至在磁盘上甚至没有对应的文件(例如,在运行时生成的禁用图标)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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