python - pyuic5 - 未知的 C++ 类 : QfontDatabase

标签 python pyqt pyqt5 qt-designer pyuic

使用 Qt Designer 5.9.5 构建了带有 QFontComboBox 的简单用户界面。当我运行 pyuic5 时,它会产生错误;

pyuic5 demoFontComboBox.ui -o demoFontComboBox.py
Unknown C++ class: QfontDatabase

.ui文件是;

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>500</width>
    <height>240</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>50</y>
     <width>81</width>
     <height>17</height>
    </rect>
   </property>
   <property name="text">
    <string>Select font</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>90</y>
     <width>81</width>
     <height>17</height>
    </rect>
   </property>
   <property name="text">
    <string>Type text</string>
   </property>
  </widget>
  <widget class="QFontComboBox" name="fontSelect">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>40</y>
     <width>291</width>
     <height>25</height>
    </rect>
   </property>
   <property name="writingSystem">
    <enum>QFontDatabase::Any</enum>
   </property>
  </widget>
  <widget class="QTextEdit" name="textEdit">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>80</y>
     <width>291</width>
     <height>101</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

生成部分.py文件如下;

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

# Form implementation generated from reading ui file 'demoFontComboBox.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(500, 240)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(30, 50, 81, 17))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(30, 90, 81, 17))
        self.label_2.setObjectName("label_2")
        self.fontSelect = QtWidgets.QFontComboBox(Dialog)
        self.fontSelect.setGeometry(QtCore.QRect(120, 40, 291, 25))

生成的代码似乎缺少 retranslatedUi() 函数声明和调用。

在尝试上面的 ui 示例之前和之后,pyuic5 已经使用 QLineEdit、QButton、QRadioButton 成功地从 ui 生成了文件。

最佳答案

看来是 PyQt5 的 bug 不识别 QFontDatabase 类,目前的解决方法是去掉下面几行:

<property name="writingSystem">
 <enum>QFontDatabase::Any</enum>
</property>

所以执行命令:

pyuic5 demoFontComboBox.ui -o demoFontComboBox.py -x

您得到以下内容:

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

# Form implementation generated from reading ui file 'demoFontComboBox.ui'
#
# Created by: PyQt5 UI code generator 5.12.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(500, 240)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(30, 50, 81, 17))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(30, 90, 81, 17))
        self.label_2.setObjectName("label_2")
        self.fontSelect = QtWidgets.QFontComboBox(Dialog)
        self.fontSelect.setGeometry(QtCore.QRect(120, 40, 291, 25))
        self.fontSelect.setObjectName("fontSelect")
        self.textEdit = QtWidgets.QTextEdit(Dialog)
        self.textEdit.setGeometry(QtCore.QRect(120, 80, 291, 101))
        self.textEdit.setObjectName("textEdit")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Select font"))
        self.label_2.setText(_translate("Dialog", "Type text"))




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

仍在为 pyqt5 打补丁。

解决方法是在PyQt5/uic/Compiler/qtproxies.py中注册QFontDatabase类:

# ...
class QtGui(ProxyNamespace):
    class QIcon(ProxyClass):
        class fromTheme(ProxyClass): pass

    class QConicalGradient(ProxyClass): pass
    class QLinearGradient(ProxyClass): pass
    class QRadialGradient(ProxyClass): pass
    class QBrush(ProxyClass): pass
    class QPainter(ProxyClass): pass
    class QPalette(ProxyClass): pass
    class QFont(ProxyClass): pass
    class QFontDatabase(ProxyClass): pass # <--- add this line
# ...

关于python - pyuic5 - 未知的 C++ 类 : QfontDatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55510469/

相关文章:

python - 从字符串添加装饰器

python - 为什么 QtGui.QSound 根本不播放任何东西?

python - 在PyQt5中使用QMessageBox重启我的游戏或退出应用程序

python - fbs 和 PyQt5 中的应用程序名称显示为未知

python - 我可以将 Paraview 的渲染器或交互器添加到我的 PyQt5 应用程序中吗?

python - 获取 B 列中 A 列中找到的字符串数,并在新列中返回值

python - 替换文本文件中的单词并将其写入python中的新文件

python - 如何设置用 PIL 绘制的句子中单个字母或单词的样式?

python - 在 Windows 上使用 pyqt 时,QProcess.pid() 结果代表什么?

python - 父级中的相对 QWidget 位置 - 不正确的 Y