我在哪里为 Qt 设计器中的单个升级的 QWidget 编写类

use*_*867 3 python pyqt qt-designer pyqt5 python-3.6

我阅读、测试并理解了很多来自 Qt 设计器的 QWidgets 使用示例,这些示例已升级到 PyQt5。尽管如此,我无法为我自己处理一个简单的例子。

下面我展示了我的代码,该代码不起作用并尝试解释。

在 Qt 设计器中,我创建了一个简单的 QMainWindow,标准化命名为 MainWindow

我在其中创建了一个标签 QLabel。我将其提升为“neuLabel”类并将其命名为 labelqt。窗口包含 Add 、标题 neulabel.h 和 Promotion --> 一切都很好。

我明白,我必须为 neuLabel 类编写代码。但是:在哪里做呢?

通过非常简单的例子,我尝试了这样的操作:

from PyQt5 import QtWidgets, uic
import sys
uifile_1 = 'testpromote.ui'
form_1, base_1 = uic.loadUiType(uifile_1)
class neuLabel(QLabel, QPixmap):
    def __init__(self,title,pixmap,parent):
    super().__init__(title,parent)
    self.setAcceptDrops(True)
def mousePressEvent(self, e):
    QLabel.mousePressEvent(self, e)
    if e.button() == QtCore.Qt.LeftButton:
        print('press')
class myApp(base_1, form_1):
    def __init__(self):
        super(base_1,self).__init__()
        self.setupUi(self)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = myApp()   #Dialog()
    ex.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

错误是这样的

Traceback (most recent call last):
  File "/home/jf/PycharmProjects/myMuhaInp/testpromote.py", line 5, in <module>
    form_1, base_1 = uic.loadUiType(uifile_1)
  File "/usr/lib/python3/dist-packages/PyQt5/uic/__init__.py", line 201, in loadUiType
    exec(code_string.getvalue(), ui_globals)
  File "<string>", line 37, in <module>
ModuleNotFoundError: No module named 'neulabel'
Run Code Online (Sandbox Code Playgroud)

我真的不明白,在哪里编码类 neulabel (我真的尝试了很多例子。(在没有例子中,我发现,是单个 QWidget 的提升)

eyl*_*esc 7

在你的所有代码出现错误之前:为什么你需要一个继承自QLabel和QPixmap的类?我不明白这一点,QLabel使用QPixmap(组合),QLabel不是QPixmap(固有)。

\n\n

对于要升级的小部件,它必须具有以下规则(我在文档中没有找到它们):

\n\n
    \n
  • 小部件的构造函数必须具有单个参数并且必须是父级。

  • \n
  • 该类不能位于使用它的同一文件中,因为它可以创建循环导入。

  • \n
  • 小部件促销表单需要 2 条数据:

    \n\n
      \n
    1. 提升的类名必须是类的名称。
    2. \n
    3. 头文件必须是类所在文件的位置(在 C++ 中,根据自定义规则,文件名与类的名称相同,但使用小写字母,但在 python 中则不符合)。

    4. \n
    \n\n

    例如,如果“FooClass”被放置为升级类名称,并且“a/b/c”或“abc”被放置在头文件中,它将被翻译以from a.b.c import Foo验证这是正确的。

  • \n
\n\n

例子:

\n\n

考虑到上述情况,最简单的实现是创建一个 .py,其中要提升的类是:

\n\n

神经标签.py

\n\n
from PyQt5 import QtCore, QtWidgets\n\n\nclass NeuLabel(QtWidgets.QLabel):\n    def __init__(self, parent=None):\n        super().__init__(parent)\n        self.setAcceptDrops(True)\n\n    def mousePressEvent(self, e):\n        super().mousePressEvent(e)\n        if e.button() == QtCore.Qt.LeftButton:\n            print("press")\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后在 .ui 中,您必须在表单中填写以下内容,按“添加”按钮,然后按“升级”按钮

\n\n

在此输入图像描述

\n\n

生成以下.ui:

\n\n
<?xml version="1.0" encoding="UTF-8"?>\n<ui version="4.0">\n <class>MainWindow</class>\n <widget class="QMainWindow" name="MainWindow">\n  <property name="geometry">\n   <rect>\n    <x>0</x>\n    <y>0</y>\n    <width>800</width>\n    <height>600</height>\n   </rect>\n  </property>\n  <property name="windowTitle">\n   <string>MainWindow</string>\n  </property>\n  <widget class="QWidget" name="centralwidget">\n   <widget class="NeuLabel" name="label">\n    <property name="geometry">\n     <rect>\n      <x>120</x>\n      <y>160</y>\n      <width>251</width>\n      <height>191</height>\n     </rect>\n    </property>\n    <property name="text">\n     <string>TextLabel</string>\n    </property>\n   </widget>\n  </widget>\n  <widget class="QMenuBar" name="menubar">\n   <property name="geometry">\n    <rect>\n     <x>0</x>\n     <y>0</y>\n     <width>800</width>\n     <height>26</height>\n    </rect>\n   </property>\n  </widget>\n  <widget class="QStatusBar" name="statusbar"/>\n </widget>\n <customwidgets>\n  <customwidget>\n   <class>NeuLabel</class>\n   <extends>QLabel</extends>\n   <header>neulabel</header>\n  </customwidget>\n </customwidgets>\n <resources/>\n <connections/>\n</ui>\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后在主文件中使用:

\n\n
import os\nimport sys\n\nfrom PyQt5 import QtWidgets, uic\n\nCURRENT_DIR = os.path.dirname(os.path.realpath(__file__))\n\nuifile_1 = os.path.join(CURRENT_DIR, "testpromote.ui")\nform_1, base_1 = uic.loadUiType(uifile_1)\n\n\nclass myApp(base_1, form_1):\n    def __init__(self):\n        super(base_1, self).__init__()\n        self.setupUi(self)\n\n\nif __name__ == "__main__":\n    app = QtWidgets.QApplication(sys.argv)\n    ex = myApp()\n    ex.show()\n    sys.exit(app.exec_())\n
Run Code Online (Sandbox Code Playgroud)\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 neulabel.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 testpromote.ui\n
Run Code Online (Sandbox Code Playgroud)\n