我阅读、测试并理解了很多 Qt 设计器中的 QWidgets 使用示例,这些示例被提升到 PyQt5。尽管如此,我还是无法为自己处理一个简单的例子。
下面我展示了我的代码,它不起作用并尝试解释。
在 Qt 设计器中我创建了一个简单的 QMainWindow,命名为 standardized as MainWindow
我在其中创建了一个标签 QLabel。我将其提升为“neuLabel”类并将其命名为 labelqt。 带有 Add 标题 neulabel.h 和 promote 的窗口 --> 一切都很好。
我明白,我必须为类 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_())
错误是这样的
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'
我真的不明白,在哪里编写类neulabel(我确实尝试了很多例子。(我发现没有一个例子是提升单个QWidget)
最佳答案
之前你所有的代码都有一个错误:为什么你需要一个继承自QLabel和QPixmap的类?我不明白这一点,QLabel 使用 QPixmap(合成),QLabel 是不是QPixmap(继承性)。
对于要推广的小部件,它必须具有以下规则(我没有在文档中找到它们):
小部件的构造函数必须有一个参数并且必须是父级。
该类不能位于使用它的同一个文件中,因为它可以创建循环导入。
小部件推广表单需要 2 个数据:
- 提升的类名,必须是类名。
- 头文件,它必须是类所在文件的位置(在 C++ 中,根据自定义规则,文件名与类名相同,但带有小写字母,但在 python 中它不符合)。
例如,如果“FooClass”被放置为提升的类名并且“a/b/c”或“a.b.c”被放置在头文件中,它将被翻译到
from a.b.c import Foo
验证这是正确的。
示例:
考虑到以上情况,最简单的实现是创建一个.py,其中要提升的类是:
neulabel.py
from PyQt5 import QtCore, QtWidgets
class NeuLabel(QtWidgets.QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
def mousePressEvent(self, e):
super().mousePressEvent(e)
if e.button() == QtCore.Qt.LeftButton:
print("press")
然后在 .ui 中,您必须在表格中填写以下内容,按“添加”按钮,然后按“提升”按钮
生成以下 .ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="NeuLabel" name="label">
<property name="geometry">
<rect>
<x>120</x>
<y>160</y>
<width>251</width>
<height>191</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>NeuLabel</class>
<extends>QLabel</extends>
<header>neulabel</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
然后在主文件中使用:
import os
import sys
from PyQt5 import QtWidgets, uic
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
uifile_1 = os.path.join(CURRENT_DIR, "testpromote.ui")
form_1, base_1 = uic.loadUiType(uifile_1)
class myApp(base_1, form_1):
def __init__(self):
super(base_1, self).__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = myApp()
ex.show()
sys.exit(app.exec_())
├── main.py
├── neulabel.py
└── testpromote.ui
关于python - 我在哪里为来自 Qt 设计器的单个提升的 QWidget 编写类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60782496/