当前位置:网站首页>(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
2022-06-25 12:36:00 【haoming Hu】
GitHub Connect :
This column all source code GitHub Through train
In the last article, we talked about how to use the signals and slots that come with the object
This one is more winding , Anyway, I saw it for the first time , I feel dizzy . Let's be clear
Use the diagram below to make it clear 
First of all, we need to know , who ( sender ) What signals are sent ( The signal ) To whom , Who receives the signal and performs what function ( Slot function )
Code first
ui File code
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '03.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(691, 509)
self.verticalLayoutWidget = QtWidgets.QWidget(Form)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(100, 60, 391, 231))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton_2 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.verticalLayout.addWidget(self.pushButton_3)
self.pushButton_4 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.verticalLayout.addWidget(self.pushButton_4)
self.pushButton_1 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_1.setObjectName("pushButton_1")
self.verticalLayout.addWidget(self.pushButton_1)
self.pushButton_5 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_5.setObjectName("pushButton_5")
self.verticalLayout.addWidget(self.pushButton_5)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(140, 340, 411, 81))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(14)
self.label.setFont(font)
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton_1.setText(_translate("Form", " No parameter "))
self.pushButton_2.setText(_translate("Form", " One int Type parameter "))
self.pushButton_3.setText(_translate("Form", "int and str There are two parameters of type "))
self.pushButton_4.setText(_translate("Form", " List as parameter "))
self.pushButton_5.setText(_translate("Form", " Dictionaries as parameters "))
self.label.setText(_translate("Form", " Custom signal and slot tests , And parameter passing "))
Master file code :
from ui3 import Ui_Form
import sys,time
from PyQt5 import QtWidgets,QtCore
class mydesigner(QtWidgets.QWidget,Ui_Form):
# Define custom signals
signal_1 = QtCore.pyqtSignal()# With no arguments
signal_2 = QtCore.pyqtSignal(int)# belt int Type parameter
signal_3 = QtCore.pyqtSignal(int,str)# belt int and str Two parameters
signal_4 = QtCore.pyqtSignal(list)# With list type
signal_5 = QtCore.pyqtSignal(dict)# Typical with character
def __init__(self):
super(mydesigner,self).__init__()
self.setupUi(self)
# Bind custom functions for custom signals , When the receiver receives this signal , This function will be executed
self.signal_1.connect(self.signal_1_response)
self.signal_2.connect(self.signal_2_response)
self.signal_3.connect(self.signal_3_response)
self.signal_4.connect(self.signal_4_response)
self.signal_5.connect(self.signal_5_response)
self.pushButton_1.clicked.connect(self.signal_1_call)
self.pushButton_2.clicked.connect(self.signal_2_call)
self.pushButton_3.clicked.connect(self.signal_3_call)
self.pushButton_4.clicked.connect(self.signal_4_call)
self.pushButton_5.clicked.connect(self.signal_5_call)
def signal_1_call(self):
self.signal_1.emit()
def signal_2_call(self):
self.signal_2.emit(1)
def signal_3_call(self):
self.signal_3.emit(1,"test")
def signal_4_call(self):
self.signal_4.emit([1,2,3,4])
def signal_5_call(self):
self.signal_5.emit({
"name":"huhaoming","age":"20"})
# Custom slot function
def signal_1_response(self):
print(" With no arguments ")
def signal_2_response(self,val):
print(" With shaping parameters :",val)
def signal_3_response(self,val_int,val_str):
print(" belt int and str Two parameters :",val_int,val_str)
def signal_4_response(self,val):
print(" With list type :",val)
def signal_5_response(self,val):
print(" Typical with character :",val)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myshow = mydesigner()
myshow.show()
sys.exit(app.exec_())
In order to express myself better, I understand , I'll use the following diagram with the code :
Just understand two lines of code
First of all :
self.pushButton_1.clicked.connect(self.signal_1_call)
This means the main interface Of Button Of Click event Connect the self.signal_1_call This function , To put it another way, when this button clicks ( Not limited to clicking , There could be other events ) I will go to perform self.signal_1_call This function , And this function is responsible for transmitting a custom signal . Here is the launch to the main interface .
second :
self.signal_1.connect(self.signal_1_response)
This means that the signal on the main interface is connected to self.signal_1_response This function , When the main interface receives this signal , Go back to execution ,self.signal It's a property of the main interface , A signal can be specified to connect a slot function , This slot function is when you transmit this signal , What you want me to do . This slot function can print out the parameters directly . It's reflected in the code
Anyway, it's very winding here , I don't know how you guys learn . It's much better to understand ( For obsessive-compulsive disorder ).
**
This code can be run directly . There's another point I forgot to say , When defining a custom signal, you need to use pyqtSignal class , So a signal can also be regarded as an object ,pyqtSignal Class in QtCore under . Import required
**
For the signal and slot function of the user-defined and its transfer parameters when temporarily learning and understanding here , There may be mistakes in this blog , For reference only . I'll come back later when I understand better .
This source code is also uploaded to gitHub The above , Connect to the top of the blog .
边栏推荐
- Execution order of MySQL query statements join, on and where
- ECSHOP quickly purchases goods, simplifies the shopping process, and improves the user experience through one-step shopping
- Ubuntu uninstalling PHP
- Zhangxiaobai's road of penetration (VI) -- the idea and process of SQL injection and the concat series functions and information of SQL_ Schema database explanation
- PHP takes the difference set of two arrays
- Spicy food advertising e-commerce system development function and spicy food advertising e-commerce app system development source code sharing
- Qiantang Pingou source code -- Qiantang Pingou app system development source code sharing
- Concise H5 error page
- 20. MVVM command binding of WPF
- Tidb common commands
猜你喜欢

An easy-to-use seal design tool - (can be converted to ofd file)

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

Linear regression of common mathematical modeling models for College Students

How to use ARIMA model for prediction?

A set of automated paperless office system (oa+ approval process) source code: with data dictionary

Upgrade opsenssh to 8.8p1

Explain factor analysis in simple terms, with case teaching (full)

15. Notes on the button style of WPF

Découvrir gaussdb (pour redis): une comparaison complète avec Codis

What is principal component analysis? Dimension reduction of classical case analysis variables
随机推荐
Zhangxiaobai's road to penetration (7) -sql injection detailed operation steps -union joint query injection
Laravel multi project mutual access
Découvrir gaussdb (pour redis): une comparaison complète avec Codis
Hook technology
Zunpin Yongyao advertising e-commerce system -- Zunpin Yongyao advertising e-commerce app system development source code sharing
When MySQL queries fields in JSON format, it takes a property value of JSON data
Total number of MySQL statistics, used and unused
Possible causes of wechat applet decryption failure
Wechat forbids sharing
Explanation of ideas and sharing of pre-processing procedures for 2021 US game D (with pre-processing data code)
thinkphp3.2.5 GIF. class. php for php7.4
Laravel echart statistical chart line chart
Fun pocket mall -- sharing the development source code of fun pocket app system
What is Flink? What can Flink do?
Kotlin study notes
An easy-to-use seal design tool - (can be converted to ofd file)
Upgrade opsenssh to 8.8p1
Polling and long polling
Micro engine remote attachment 7 Niu cloud upload
Installation and removal of MySQL under Windows