問題描述
我閱讀、測試和理解了很多 Qt 設(shè)計器的 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 設(shè)計器中,我創(chuàng)建了一個簡單的 QMainWindow,命名為標準化的 MainWindow
in Qt desginer i create a simple QMainWindow, named standardized as MainWindow
在我創(chuàng)建單個標簽 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進行編碼(我確實嘗試了很多示例.(在沒有示例中,我發(fā)現(xiàn),是單個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)
推薦答案
在你所有的代碼出現(xiàn)錯誤之前:為什么需要一個繼承自 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).
對于要提升的小部件,它必須具有以下規(guī)則(我在文檔中沒有找到它們):
For a widget to be promoted it must have the following rules (I did not find them in the docs):
小部件的構(gòu)造函數(shù)必須有一個參數(shù),并且必須是父級.
The constructor of the widget must have a single parameter and must be the parent.
該類不能在使用它的同一文件中,因為它可以創(chuàng)建循環(huán)導入.
The class must not be in the same file where it is used since it can create a circular import.
小部件促銷表單要求提供 2 個數(shù)據(jù):
The widget promotion form asks for 2 data:
- 提升的類名,必須是類名.
- 頭文件 必須是類所在文件的位置(在 C++ 中,根據(jù)自定義規(guī)則,文件名與類名相同,但使用小寫字母,但在 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.
示例:
考慮到上述情況,最簡單的實現(xiàn)是創(chuàng)建一個.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中你必須在表格中填寫以下內(nèi)容,按添加"按鈕,然后按推廣"按鈕
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
這篇關(guān)于我在哪里為 Qt 設(shè)計器的單個提升 QWidget 編寫類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!