【问题标题】:PyQt4 not loading icons from .uiPyQt4没有从.ui加载图标
【发布时间】:2012-09-12 23:50:20
【问题描述】:

我在 PyQt4 中遇到了图标问题:

我使用 Qt Designer 创建了一个 UI,添加了一些带有系统主题图标的按钮,然后在 Python 程序中加载了 .ui 文件,但图标不可见(按钮显示它们的文本)。

如果我使用 QIcon.fromTheme 它可以工作,但它不会加载 .ui 文件中定义的图标。

如何让它加载这些图标,而无需从代码中手动加载它们?

【问题讨论】:

  • 如果我创建一个按钮并使用“从主题设置图标...”对我有用,然后使用pyuic4 -x test.ui 生成一个可运行的脚本。你用什么代码来加载你的 ui 文件?
  • 我正在使用 uic 模块 (uic.loadUi("main.ui",self)) 因为 pyuic4 为我创建了一个空文件
  • 听起来你可能在某处做错了什么。请参阅下面的分步回答 - 它对您有用吗?
  • 我之前写过,pyuic创建了一个空文件(不是真的空,导入后就结束了),所以我直接用PyQt4.uic模块导入
  • 经过进一步调查,PyQt 中似乎存在错误(请参阅我更新的答案)。至于pyuic 创建一个“空”文件:你肯定做错了什么。您是否尝试使用我的答案中的代码?

标签: python qt icons pyqt4


【解决方案1】:

更新

uic 包中似乎存在一个错误,该错误会阻止在使用 uic.loadUi 时加载主题图标。

违规代码在 PyQt4/uic/icon_cache.py 中(第 60 行左右):

# Handle a themed icon.
theme = iconset.attrib.get('theme')
if theme is not None:
    return self._object_factory.createQObject("QIcon.fromTheme",
            'icon', (as_string(theme), ), is_attribute=False)

问题是使用as_string 函数,它试图通过,例如'_fromUtf8("face-smile")'QIcon.fromTheme 而不是简单的图标名称。

如果您想解决这个问题,我建议您在 PyQt4 mailing list 上报告它。

请注意,等效功能在 PySide 中有效:

from PySide.QtUiTools import QUiLoader

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        QUiLoader().load('/tmp/test.ui', self)

以下是如何将pyuic4 与在 Qt Designer 中创建的 ui 一起使用的简单细分。

首先,创建您的用户界面。下面是一个最小的ui 文件,它创建了一个带有主题图标的按钮:

/tmp/test.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>100</y>
     <width>92</width>
     <height>25</height>
    </rect>
   </property>
   <property name="text">
    <string>PushButton</string>
   </property>
   <property name="icon">
    <iconset theme="face-smile"/>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

接下来,使用pyuic4ui 文件中编译一个python 模块:

pyuic4 -o /tmp/test_ui.py /tmp/test.ui

/tmp/test_ui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '/tmp/test.ui'
#
# Created: Fri Sep 21 16:45:39 2012
#      by: PyQt4 UI code generator 4.9.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

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

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(110, 100, 92, 25))
        icon = QtGui.QIcon.fromTheme(_fromUtf8("face-smile"))
        self.pushButton.setIcon(icon)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))

现在将编译好的 ui 类导入你的应用程序:

/tmp/test.py

from PyQt4 import QtGui, QtCore
from test_ui import Ui_Form

class Window(QtGui.QWidget, Ui_Form):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

最后,运行应用程序:

python2.7 /tmp/test.py

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    相关资源
    最近更新 更多