当前位置:网站首页>Pyqt GUI interface and logic separation
Pyqt GUI interface and logic separation
2022-07-07 22:27:00 【Eva215665】
analysis Visual design form Qt Designer Design GUI Single form program This example , You can find , It achieved the right Ui_FormHello
Use , Generated GUI Program , But it's flawed . because appMain1.py It's completely a procedural procedure , It is difficult to realize the effective encapsulation of business logic and functions . This paper introduces the design method of separating single inheritance interface from business logic .
Still in Visual design form Qt Designer Design GUI Single form program Used in the example project Next , Write another file appMain.py
, In this file
(1) Define the form business logic class QmyWidget
, Single inheritance QWidget
class
(2) stay QmyWidget
In the constructor of , First, call the constructor of the parent class , such self It's already a QWidget
Object
(3) Show create a Ui_FormHello
Private properties of class self.__ui
, This private property contains all components on the form , Only through this object can the component be accessed , Including calling setupUi function
(4)self.__ui It's a private property , Cannot access outside of class , To access components , You need to define interface functions to realize a series of functions . For example, designing public functions setBtnText
Used to set the text on the button
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from FormHello import Ui_FormHello
class QmyWidget (QWidget):
def __init__(self, parent = None):
super().__init__(parent) # adopt super Call parent constructor , establish QWidget forms , such self It's just a form object
self.__ui = Ui_FormHello() # establish UI object , Private property __ui Including visual design UI All components on the form , So only through
# self.__ui To access the components on the form , Including calling setupUi function
# and __ui It's a private property , Create objects outside the class , You cannot access components on the form through objects , To access components , You can define interfaces , Realization function
self.__ui.setupUi(self) # establish UI
self.__ui.LabHello.setText(" Single inherited QmyWidget")
# Definition setBtnText The interface function , Used to set the text on the button
def setBtnText(self, aText):
self.__ui.btnClose.setText(aText)
# establish app
if __name__ == "__main__":
app = QApplication(sys.argv) # establish app, use QApplication class
myWidget = QmyWidget()
myWidget.setBtnText(" Indirect settings ")
myWidget.show()
sys.exit(app.exec_())
边栏推荐
- DBSync新增对MongoDB、ES的支持
- Revit secondary development - Hide occlusion elements
- Use json Stringify() to realize deep copy, be careful, there may be a huge hole
- Revit secondary development - project file to family file
- Cataloger integrates lidar and IMU for 2D mapping
- UWA问答精选
- What is the difference between the three values of null Nan undefined in JS
- Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
- How to choose the appropriate automated testing tools?
- Remember that a development is encountered in the pit of origin string sorting
猜你喜欢
How to make agile digital transformation strategy for manufacturing enterprises
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
[JDBC Part 1] overview, get connection, CRUD
Px4 autonomous flight
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
Jerry's about TWS channel configuration [chapter]
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
使用 BlocConsumer 同时构建响应式组件和监听状态
Matplotlib drawing interface settings
随机推荐
Remember that a development is encountered in the pit of origin string sorting
How to judge whether the input content is "number"
How to turn on win11 game mode? How to turn on game mode in win11
Node:504 error reporting
The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
Write in front -- Talking about program development
Relationship between URL and URI
Kaggle-Titanic
Crawler (17) - Interview (2) | crawler interview question bank
It's worth seeing. Interview sites and interview skills
Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
Unity development --- the mouse controls the camera to move, rotate and zoom
Revit secondary development - project file to family file
SAR影像质量评估
SAR image quality evaluation
TCP/IP 协议栈
Blender exchange group, welcome to the water group ~