当前位置:网站首页>(3) Pyqt5 tutorial -- > signal and slot preliminary test
(3) Pyqt5 tutorial -- > signal and slot preliminary test
2022-06-25 12:36:00 【haoming Hu】
GitHub Connect :
This column all source code GitHub Through train
The signal and slot are QT The core mechanism of , It's a high-level interface , Applied to communication between objects , It is QT The core feature of , It's also QT The important difference from other toolkits . It automatically generates the additional code needed for high-level event handling . For example, in tkinter A space in the module There is a callback function that responds to every action they can trigger , This callback function is usually a pointer to a function . however , stay QT Signals and slots in replace these function pointers .
all QObject Class can use the signal slot , Inherited from pyqt Basically, all classes in can use the signal slot mechanism .
At present, it can be understood as : The signal is the trigger of time , And then connect this signal to the slot ( Take retail as an example , It could be something else ) Connect , for instance : I click a button to close the window , At this time, the button click event is a signal , The window closing is the processing time after the slot receives the signal . Let's look at the following examples .
There are two ways to simply implement slots . The first is in QTdesigner Design the signal and slot well , The other is to connect with code .
The first one is :
stay QTdesigner The connection between signal and slot is realized in 
There are two objects here :pushbutton Button and form window , Click from the button to pull a line to form It's specified anywhere that the signal will come from pushbutton Generate and send to form This object . After connecting , The pop-up window is the interface for configuring signals , I'll give it to push The configured signal is the click event , then form Object will run after it is received closed function , This time the window will close .
The following figure shows the relationship between the signal and the slot , And the sender and the receiver 
QTdesigner Generated py Code :
stay QTdesigner The connection in is embodied in self.pushButton.clicked.connect(Form.close) This line of code .
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '02.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(748, 523)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(90, 160, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
self.pushButton.clicked.connect(Form.close)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "pushButton"))
Master file code :
from ui2 import Ui_Form# Import on QTdesigner Well designed ui.py file
import sys
from PyQt5 import QtWidgets
class mydesigner(QtWidgets.QWidget,Ui_Form):
def __init__(self):
super(mydesigner,self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.signaltest)
def signaltest(self):
print("haomingHu")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myshow = mydesigner()
myshow.show()
sys.exit(app.exec_())
The code implementation connection is also very simple , To be clear, this slot can be an intrinsic member function of an object or a custom function , This custom function can be class or individual .
self.pushButton.clicked.connect(self.signaltest)
def signaltest(self):
print("haomingHu")
Is to customize a print function , When you click pushbutton Button time , You can put the button's click The signal is sent to form, After receiving it, you can execute the bound event .( The function used is connect function , Bound functions don't need to use (), You just need a function name )
question: What if you need to transfer parameters ? This one is a little bit winding , Let's make this clear. Please supplement it or put it in the next article .
边栏推荐
- apple 为什么要改 objc_msgSend 的类型申明
- Huikan source code -- Huikan app system secondary development source code sharing
- Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing
- PHP numeric array sorting and associative array sorting
- Polling and long polling
- The R language cartools package divides data, the scale function scales data, and the NaiveBayes function of e1071 package constructs a naive Bayesian model
- Uncover gaussdb (for redis): comprehensive comparison of CODIS
- Qiantang Pingou source code -- Qiantang Pingou app system development source code sharing
- Figure explanation of fiborache sequence
- Laravel is not logged in and cannot access the background by entering the URL
猜你喜欢

Jeecgboot startup popup configuration is still incorrect

Installation and removal of MySQL under Windows

Time series analysis - how to use unit root test (ADF) correctly?

Upgrade opsenssh to 8.8p1

What is Flink? What can Flink do?

20. MVVM command binding of WPF

The server reported an error 503 service unavailable:the system returned: (71) protocol error

Service charge and time setting code sharing involved in crmeb withdrawal process

揭秘GaussDB(for Redis):全面对比Codis

Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection
随机推荐
Digital currency exchange -- digital currency exchange system development source code sharing
Kotlin study notes
The dist function of R language calculates the distance between two samples in dataframe data, returns the distance matrix between samples, and specifies the distance calculation method through the me
Explanation of ideas and sharing of pre-processing procedures for 2021 US game D (with pre-processing data code)
一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推
Install MySQL client
Initialize the project using the express framework
ARM V7 连续加载/存储
The source code of the hottest online disk money making system in 2022 imitates Lanzou online disk / Chengtong online disk / sharing money making cloud disk system / online disk VIP Download System
ThinkPHP upload image compression size
The whole page turns gray
Laravel is not logged in and cannot access the background by entering the URL
Mpai data science platform SVM support vector machine classification \ explanation of regression parameter adjustment
Arm V7 continuous load / store
Understanding and construction of devsecops and Devops
20. MVVM command binding of WPF
Array reorder based on a field
When MySQL queries fields in JSON format, it takes a property value of JSON data
How to use ARIMA model for prediction?
【数据中台】数据中台的OneID是个什么鬼,主数据它不香吗?