当前位置:网站首页>(5) Pyqt5 ---- another method of connecting signals and slots

(5) Pyqt5 ---- another method of connecting signals and slots

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 customize signals and slots, and how to use and transfer parameters

It's a little strange
But after learning this way , I don't think it's that flexible, right , This will be mentioned later

Look at the code first
Main program code :

""" /******************************************************************************** * @Filename:  Decorator signal and slot .py * @Author: haomingHu * @Version: 1.0 * @Date: 2020-12-16 * @Description: * @History: ********************************************************************************/ """
from ui4 import Ui_Form# Import on QTdesigner Well designed  ui.py  file 
import sys
from PyQt5 import QtWidgets,QtCore

class mydesigner(QtWidgets.QWidget,Ui_Form):
    def __init__(self):
        super(mydesigner,self).__init__()
        self.setupUi(self)
        

        QtCore.QMetaObject.connectSlotsByName(self)

    @QtCore.pyqtSlot()
    def on_open_clicked(self):
        self.calendarWidget.close()

    @QtCore.pyqtSlot()
    def on_close_clicked(self):
        self.calendarWidget.show()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myshow = mydesigner()
    myshow.show()
    sys.exit(app.exec_())

ui File code :

""" /******************************************************************************** * @Filename: ui4.py * @Author: haomingHu * @Version: 1.0 * @Date: 2020-12-16 * @Description: * @History: ********************************************************************************/ """


# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui4.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(432, 325)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(40, 220, 75, 23))
        self.pushButton.setObjectName("open")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(290, 220, 75, 23))
        self.pushButton_2.setObjectName("close")
        self.calendarWidget = QtWidgets.QCalendarWidget(Form)
        self.calendarWidget.setGeometry(QtCore.QRect(80, 10, 248, 197))
        self.calendarWidget.setObjectName("calendarWidget")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))
        self.pushButton_2.setText(_translate("Form", "PushButton"))

Just look at the rest of the code , Look at the following lines of code :


    @QtCore.pyqtSlot()
    def on_open_clicked(self):
        self.calendarWidget.close()

    @QtCore.pyqtSlot()
    def on_close_clicked(self):
        self.calendarWidget.show()

First , The premise of using this method is to execute the following line of code first ,( This line of code is in setupUi The function has )

QtCore.QMetaObject.connectSlotsByName(Form)

This line of code is pyqt5 Automatically connect to the core code of slot function according to the signal name in , This line of code is used to put QObject Some of the signals of a neutron object follow its objextName Connect to the corresponding slot function summary ,

Secondly, the connection format is :
@QtCore.pyqtSlot()
def on_objectSignal_function(self):

objectName It's the name of the control
function The signal of the control to be connected , Here is the click signal clicked

One problem with whimsy is , If my objectName It's a Chinese name , So this one functionName Isn't it very awkward !!

ok , That's all , As for this method, how to produce parameters , It's the same feeling emit equally , About the same !

原网站

版权声明
本文为[haoming Hu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200528576903.html