当前位置:网站首页>(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 .
边栏推荐
- Why do we do autocorrelation analysis? Explain application scenarios and specific operations
- 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 appends the same elements to a two-dimensional array
- 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
- Huile optimization system -- sharing of secondary development source code of huile optimization app system
- MySQL common interview questions
- 揭秘GaussDB(for Redis):全面对比Codis
- ARM 立即数
- Explain factor analysis in simple terms, with case teaching (full)
- Arm V7 coprocessor
猜你喜欢

Zhangxiaobai's road to penetration (7) -sql injection detailed operation steps -union joint query injection

Zhangxiaobai's way of penetration (V) -- detailed explanation of upload vulnerability and parsing vulnerability

ECSHOP commodity page multi-attribute batch purchase plug-ins ECSHOP wholesale plug-ins multi-attribute order placing, multi-attribute batch purchase of commodities

Installation and removal of MySQL under Windows

【数据中台】数据中台的OneID是个什么鬼,主数据它不香吗?

19. Implementation of MVVM architecture based on WPF event to command

What is the primordial universe

laravel 9

How to use ARIMA model for prediction?

Dark horse shopping mall ---6 Brand, specification statistics, condition filtering, paging sorting, highlighting
随机推荐
Is it safe to open an account and buy stocks on the Internet?
MySQL common interview questions
JQ dynamic setting radio does not take effect when selected
VIM common commands and shortcut keys
Thinkphp3 reinforcement i() function filter single quotation marks
mysql FIND_ IN_ Set function
20. MVVM command binding of WPF
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
Lighten the source code -- lighten the app system development function introduction to the beautiful world lighten the app system development source code in China
Laravel echart statistical chart line chart
Laravel task scheduling
When MySQL queries fields in JSON format, it takes a property value of JSON data
Dark horse shopping mall ---6 Brand, specification statistics, condition filtering, paging sorting, highlighting
Arm V7 coprocessor
Why do we do autocorrelation analysis? Explain application scenarios and specific operations
ECSHOP quickly purchases goods, simplifies the shopping process, and improves the user experience through one-step shopping
If you also want to be we media, you might as well listen to Da Zhou's advice
Mind mapping video
Installation and removal of MySQL under Windows
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