我在哪里为 Qt 设计器的单个提升 QWidget 编写类
问题描述
我阅读、测试和理解了很多 Qt 设计器的 QWidgets 使用示例,这些示例被提升为 PyQt5.尽管如此,我无法为自己处理一个简单的例子.
I read, test and understand a lot of examples for usage of QWidgets from Qt designer, which are promoted to PyQt5. Nevertheless I am not able to deal with a simple example for my own.
下面我展示了我的代码,它不起作用并尝试解释.
Beneath I show my code, which is not working and try to explain.
在 Qt 设计器中,我创建了一个简单的 QMainWindow,命名为标准化的 MainWindow
in Qt desginer i create a simple QMainWindow, named standardized as MainWindow
在我创建单个标签 QLabel.我将其提升为neuLabel"类并将其命名为 labelqt.带有 Add 、标题 neulabel.h 和提升的窗口 --> 一切都很好.
Within I create a single label QLabel. This I promote to class "neuLabel" and name it labelqt. The window with, Add , the header neulabel.h and promote --> everything is fine.
我明白,我必须为类 neuLabel 编写代码.但是:在哪里做呢?
I understand, that I have to write code for the class neuLabel. But: WHERE to do it?
通过非常简单的示例,我尝试了这样的:
With very simple example I tried it like this:
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的推广)
I really don't understand, WHERE to code the class neulabel (and I really tried a lot of examples. (In no example, I found, is the promotion of a single QWidget)
解决方案
在你所有的代码出现错误之前:为什么需要一个继承自 QLabel 和 QPixmap 的类?我不明白这一点,QLabel 使用 QPixmap(composition),QLabel is 不是 QPixmap(inherency).
Before all your code has an error: Why do you need a class that inherits from QLabel and QPixmap? I don't see the point, a QLabel uses a QPixmap(composition), a QLabel is not a QPixmap(inherency).
对于要提升的小部件,它必须具有以下规则(我在文档中没有找到它们):
For a widget to be promoted it must have the following rules (I did not find them in the docs):
小部件的构造函数必须有一个参数,并且必须是父级.
The constructor of the widget must have a single parameter and must be the parent.
该类不能在使用它的同一文件中,因为它可以创建循环导入.
The class must not be in the same file where it is used since it can create a circular import.
小部件促销表单要求提供 2 个数据:
The widget promotion form asks for 2 data:
- 提升的类名,必须是类名.
- 头文件 必须是类所在文件的位置(在 C++ 中,根据自定义规则,文件名与类名相同,但使用小写字母,但在 python 中不符合).
- Promoted class name which must be the name of the class.
- Header file which must be the location of the file where the class is (in C++ by custom rule the name of the file is the same name of the class but with lowercase letters, but in python it does not comply).
例如,如果FooClass"被放置为Promoted class name并且a/b/c"或abc"被放置在头文件中,它将被翻译到 from abc import Foo
确认这是正确的.
For example if "FooClass" is placed as Promoted class name and "a/b/c" or "a.b.c" is placed in Header file it will be translated to from a.b.c import Foo
so verify that this is correct.
示例:
考虑到上述情况,最简单的实现是创建一个.py,其中要提升的类是:
Considering the above, the simplest implementation is to create a .py where the class to be promoted is:
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中你必须在表格中填写以下内容,按添加"按钮,然后按推广"按钮
Then in the .ui you must fill in the following in the form, press the "Add" button and then the "Promote" button
生成以下 .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
相关文章