QIcon.fromTheme不在PyQt中显示图标 [英] QIcon.fromTheme not displaying icon in PyQt

查看:835
本文介绍了QIcon.fromTheme不在PyQt中显示图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示从当前图标主题中选择的QAction的图标时遇到了麻烦。我用Qt设计师制作了ui并用 pyuic4 sample.ui>导出了它。 sample.py 。使用 self.actionSample.setIcon(QtGui.QIcon.fromTheme(_fromUtf8(document-open)))设置主题中的图标后,我获得以下源代码:

I am having troubles to display the icon of a QAction selected from the current icon theme. I made the ui with Qt designer and exported it with pyuic4 sample.ui > sample.py. After setting the icon from the theme with self.actionSample.setIcon(QtGui.QIcon.fromTheme(_fromUtf8("document-open"))), I get the following source code :

from PyQt4 import QtCore, QtGui
import sys

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.toolBar = QtGui.QToolBar(MainWindow)
        self.toolBar.setObjectName(_fromUtf8("toolBar"))
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
        self.actionSample = QtGui.QAction(MainWindow)
        self.actionSample.setObjectName(_fromUtf8("actionSample"))
        self.actionSample.setIcon(QtGui.QIcon.fromTheme(_fromUtf8("document-open")))
        self.toolBar.addAction(self.actionSample)

        QtCore.QMetaObject.connectSlotsByName(MainWindow)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(window)
    window.show()
    app.exec_()

当我执行它时,工具栏不会显示图标'document-open'。这是一个错误还是我做错了什么?

When I execute it, the toolbar does not display the icon 'document-open'. Is it a bug or am I doing something wrong ?

谢谢

推荐答案

QIcon.fromTheme 使用的图标查找过程有点复杂。

The icon lookup process used by QIcon.fromTheme is somewhat complex.

图标主题仅直接支持适用于GNOME和KDE桌面环境的X11平台。对于其他平台/桌面,有必要与应用程序一起安装主题,或以各种方式调整用户的环境。

Icon themes are only directly supported on the X11 platform for the GNOME and KDE desktop environments. For other platforms/desktops, it will be necessary to either install a theme along with the application, or tweak the user's environment in various ways.

查找当前系统主题对于GNOME,Qt将查询gconf(如果gtk样式可用),否则默认为gnome主题。对于KDE,Qt将检查 kdeglobals 设置文件,否则默认为氧气(或早期版本的KDE的crystalsvg)。此外,hicolor主题始终可以用作后备。

To find the current system theme for GNOME, Qt will query gconf (if the gtk style is available), and otherwise default to the "gnome" theme. For KDE, Qt will examine the kdeglobals settings file(s), and otherwise default to "oxygen" (or "crystalsvg" for earlier versions of KDE). In addition, the "hicolor" theme can always be used as a fallback.

确定系统主题名称后,Qt将查找包含图标的目录各种预先确定的地方,这又取决于所使用的平台和桌面。在X11上,这包括 $ HOME / .icons $ XDG_DATA_DIRS / icons 。所有平台/桌面共有的唯一位置是资源路径:/ icons

Once the system theme name has been determined, Qt will look for the directory containing the icons in various pre-determined places, which again depend on both the platform and desktop being used. On X11, this includes $HOME/.icons and $XDG_DATA_DIRS/icons. The only location common to all platforms/desktops is the resource path :/icons.

图标主题目录必须包含一个 index.theme 文件,其中(除其他外)指定了可以包含图标的子目录。仅使用带有 png svg 扩展名的图标文件。

Icon theme directories must include an index.theme file, which (amongst other things) specifies the subdirectories that can contain icons. Only icon files with a png or svg extension are used.

QIcon 类有一些静态方法可以准确显示Qt在哪里寻找主题图标:

The QIcon class has some static methods which will show exactly where Qt is looking for themed icons:

>>> from PyQt4 import QtGui
>>> app = QtGui.QApplication([])
>>> for path in QtGui.QIcon.themeSearchPaths():
...     print "%s/%s" % (path, QtGui.QIcon.themeName())
... 
/home/ekhumoro/.icons/hicolor
/usr/local/share/icons/hicolor
/usr/share/icons/hicolor
:/icons/hicolor

如果没有显示文档打开图标,Qt要么是在错误的位置,要么是图标完全失踪。

If the "document-open" icon isn't being displayed, Qt is either looking in the "wrong" place, or the icon is missing altogether.

更新:

如上所述:默认情况下,Qt仅在X11平台上支持GNOME和KDE。它对FluxBox WM一无所知,因此无法检测当前的图标主题。这意味着它将回归到使用最小的hicolor主题,该主题可能没有所有必需的图标。

As I said above: by default, Qt only supports GNOME and KDE on the X11 platform. It doesn't know anything about FluxBox WM, and so cannot detect the current icon theme. This means it will fall back to using a minimal "hicolor" theme, which may not have all the required icons.

解决此问题的一种方法是创建一个hicolor符号链接指向您要使用的主题。理想情况下,这应该位于Qt搜索路径列表中的第一个位置:

One way to solve this issue, is to create a "hicolor" symlink that points to the theme you want to use. Ideally, this should go in the location that is first in Qt's list of search paths:

$ ln -s icon/theme/directory $HOME/.icons/hicolor

更新2

Qt5默认只支持kde和gnome,但 Qt Platform Abstraction layer 至少可以创建自定义主题插件( LXQT 是一个桌面利用这个的环境)。还有几个DE现在被视为gtk / gnome:X-CINNAMON,UNITY,MATE,XFCE和LXDE。

Qt5 still only supports kde and gnome by default, but the Qt Platform Abstraction layer at least makes it possible to create custom theme plugins (LXQT is one desktop environment that takes advantage of this). There are also several more DE's that are now treated as gtk/gnome: X-CINNAMON, UNITY, MATE, XFCE and LXDE.

这篇关于QIcon.fromTheme不在PyQt中显示图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆