簡體   English   中英

pyuic5-未知的C ++類:QfontDatabase

[英]pyuic5 - Unknown C++ class: QfontDatabase

使用Qt Designer 5.9.5使用QFontComboBox構建了一個簡單的ui。 當我運行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錯誤,無法識別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
# ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM