我在哪里为 Qt 设计器中的单个提升的 QWidget 写 class
where do I write the class for a single promoted QWidget from Qt designer
我阅读、测试并理解了很多来自Qt designer的QWidgets使用示例,这些示例被提升到PyQt5。然而,我自己无法处理一个简单的例子。
下面我展示了我的代码,它不起作用并尝试解释。
在 Qt 设计器中我创建了一个简单的 QMainWindow,命名为标准化的 MainWindow
我在其中创建了一个标签 QLabel。这个我提升为 class "neuLabel" 并将其命名为 labelqt。
window 添加 header neulabel.h 并提升 --> 一切正常。
我知道,我必须为 class 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'
我真的不明白,在哪里编码class neulabel(我确实尝试了很多例子。(我发现没有例子,是提升单个QWidget)
之前你所有的代码都有一个错误:为什么你需要一个继承自QLabel和QPixmap的class?我不明白这一点,QLabel uses QPixmap(composition),QLabel is not a QPixmap(inherency).
要推广的小部件必须具有以下规则(我没有在文档中找到它们):
小部件的构造函数必须有一个参数并且必须是父级。
class不能在使用它的同一个文件中,因为它可以创建循环导入。
小部件推广表单要求2个数据:
- Promoted class name 必须是 class.
的名字
- 头文件 必须是 class 所在文件的位置(在 C++ 中,根据自定义规则,文件名与class 但用小写字母,但在 python 中不符合)。
例如,如果 "FooClass" 被放置为 Promoted class name 并且 "a/b/c" 或 "a.b.c" 被放置在 头文件它会被翻译成from a.b.c import Foo
所以验证这是正确的。
示例:
考虑到以上情况,最简单的实现是创建一个.py,其中要提升的class是:
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你必须在表格中填写以下内容,按"Add"按钮然后按"Promote"按钮
正在生成以下内容。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
我阅读、测试并理解了很多来自Qt designer的QWidgets使用示例,这些示例被提升到PyQt5。然而,我自己无法处理一个简单的例子。
下面我展示了我的代码,它不起作用并尝试解释。
在 Qt 设计器中我创建了一个简单的 QMainWindow,命名为标准化的 MainWindow
我在其中创建了一个标签 QLabel。这个我提升为 class "neuLabel" 并将其命名为 labelqt。 window 添加 header neulabel.h 并提升 --> 一切正常。
我知道,我必须为 class 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'
我真的不明白,在哪里编码class neulabel(我确实尝试了很多例子。(我发现没有例子,是提升单个QWidget)
之前你所有的代码都有一个错误:为什么你需要一个继承自QLabel和QPixmap的class?我不明白这一点,QLabel uses QPixmap(composition),QLabel is not a QPixmap(inherency).
要推广的小部件必须具有以下规则(我没有在文档中找到它们):
小部件的构造函数必须有一个参数并且必须是父级。
class不能在使用它的同一个文件中,因为它可以创建循环导入。
小部件推广表单要求2个数据:
- Promoted class name 必须是 class. 的名字
- 头文件 必须是 class 所在文件的位置(在 C++ 中,根据自定义规则,文件名与class 但用小写字母,但在 python 中不符合)。
例如,如果 "FooClass" 被放置为 Promoted class name 并且 "a/b/c" 或 "a.b.c" 被放置在 头文件它会被翻译成
from a.b.c import Foo
所以验证这是正确的。
示例:
考虑到以上情况,最简单的实现是创建一个.py,其中要提升的class是:
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你必须在表格中填写以下内容,按"Add"按钮然后按"Promote"按钮
正在生成以下内容。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