[PyQt introductory tutorial] PyQt5 signals and slots

   Signals and slots are PyQt mechanism for communication between programming objects. Every inherited from QWideget controls are supported signals and slots mechanism. When the signal transmission (transmission request), the slot function is performed automatically connected (processing request). This paper describes the basic signals and slots, the most frequently used method. And a built-in signal is used to use grooves.

Built-in signal and slots

  The so-called built-in signal used slots. Refers to when the transmitted signal, using the window control function, instead of the custom functions. The method of connecting the signal channel is connected to a signal QObject slot function by another QObject QObject.signal.connect.

  In any GUI design, the button is the most important and commonly used trigger action request way to interact with the user. Common buttons include QPushButton, QRadioButton and QCheckBox. These buttons are inherited from QAbstractButton class, QAbstractButton signals include:

  Clicked : Click the left mouse button and release the trigger signal. Most used. Remember this almost enough.

  Pressed: The trigger signal when the left mouse button pressed

  Released: The trigger signal when the left mouse button is released

  Toggled: The trigger signal when the control tag changes state.

 Example built-in signal and slots

  Click here to implement a button to exit the interface implementation process needs to introduce built-in signal and slots. He started working. . .

  Step1: Open Qt Designer, select the Widget Templates. In the toolbox drag Push Button control buttons to the main screen. And modify the control display name. Save as singal.ui. Interface is as follows:

Step2: Use pyuic5 -o singal.py singal.ui .py converted format.

Step3: to consider the convenience of presentation, the program will be called in the main program in singal.py. Which MyMainForm command line class Push Button Click the button to add the signal slot function. as follows

  self.pushButton.clicked.connect(self.close)

Following complete code (direct copy operation, part of the font bold addition section):

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

# Form implementation generated from reading ui file 'signal.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(431, 166)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(160, 50, 91, 41))
        font = QtGui.QFont()
        font.setFamily("YaHei Consolas Hybrid")
        font.setPointSize(14)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "信号与槽"))
        self.pushButton.setText(_translate("Form", "关闭"))

class MyMainForm(QMainWindow, Ui_Form):
    def __init__(self, parent=None):
        super(MyMainForm, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.close)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyMainForm()
    myWin.show()
    sys.exit(app.exec_())

Run and click the following button to close the window

Custom built-in signal and grooves Example

  Like the above-described process steps to achieve. ShowMsg slot function as a custom function.

  Signals and slots: self.pushButton.clicked.connect (self.showMsg)

Following complete code (direct copy operation, part of the font bold addition section):

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

# Form implementation generated from reading ui file 'signal.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(431, 166)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(160, 50, 91, 41))
        font = QtGui.QFont()
        font.setFamily("YaHei Consolas Hybrid")
        font.setPointSize(14)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "信号与槽"))
        self.pushButton.setText(_translate("Form", "运行"))

class MyMainForm(QMainWindow, Ui_Form):
    def __init__(self, parent=None):
        super(MyMainForm, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.showMsg)

    def showMsg(self):
        QMessageBox.information(self, "信息提示框", "OK,内置信号与自定义槽函数!")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyMainForm()
    myWin.show()
    sys.exit(app.exec_())

运行结果如下:

Qt Designer添加控件信号与槽

  上述介绍的内容是通过代码方式实现内置信号与槽的连接。那Qt Designer工具可以实现信号与槽的连接?之前在第二节课Qt Designer主界面介绍时提过信号槽区域。一直没有讲如何使用。通过这个区域功能是可以实现信号与槽的连接的。

  还是以添加内置信号与槽来介绍。

  Step1:打开Qt Designer界面,找到信号槽编辑区。如下

  Step2:点击+号 Sender控件选择"PushButton"、Signal信号选择"clicked",Receiver选择"Form",内置槽函数选择"close()"

 Step3:保存.ui格式,并使用pyuic转换成.py格式,添加调用程序,运行。效果一样。这些步骤都介绍过,不再重复介绍,关键代码如下:

小结

  本文介绍了PyQt5信号与槽最基本的使用方法。知道如何在Qt Designer生成的.py文件中添加控件信号与槽的关系并且知道如何调用自定义槽函数。掌握了这些,应该就可以动手实现一些基本的需求了。

  到这里,按照这几个章节的介绍应该可以动手完成简单需求的实现。至于更进一步的学习,可以通过实践过程中遇到的问题以及小工具开发需求去驱动加深理解可能效果会好一点。

Guess you like

Origin www.cnblogs.com/linyfeng/p/11273062.html