PyQt5入门(三)——信号与槽

此总结主要参考下面这篇文章:PyQt5事件和信号

例子1、2的self继承自QWidget, 1继承自QWidget是因为vbox是QWidget类独有的

1. 信号槽 Signals & slots

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication)
# 这里导入的包并不都是非必须的,只是对于展示信号槽功能这个示例来说是有必要的


lcd = QLCDNumber(self)
sld = QSlider(Qt.Horizontal, self)

vbox = QVBoxLayout() # 垂直布局
vbox.addWidget(lcd)
vbox.addWidget(sld)

self.setLayout(vbox)
# 形如Object.signal.connect()
sld.valueChanged.connect(lcd.display) 

2. 重新实现事件函数

def keyPressEvent(self,e):

    if e.key() == Qt.Key_Escape:
        self.close()

# keyPressEvent是已经存在的函数,这里把他重新改写
# 除了示例中的这个,还有mousePressEvent()、paintEvent()等

综合前面两个功能,得到如下示例:

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

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider,
                             QVBoxLayout, QApplication, QMainWindow)


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        lcd = QLCDNumber(self)
        sld = QSlider(Qt.Horizontal, self)

        vbox = QVBoxLayout()
        vbox.addWidget(lcd)
        vbox.addWidget(sld)

        self.setLayout(vbox)
        sld.valueChanged.connect(lcd.display)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Signal & slot')
        self.show()

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Escape:
            self.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

例子3、4的self继承自QMainWindow因为后面用到的statusBar(状态栏)是该类独有的

3. 事件发送者

btn1 = QPushButton("Button 1", self)
btn2 = QPushButton("Button 2", self)

btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)

self.statusBar()

def buttonClicked(self):

    sender = self.sender()
    self.statusBar().showMessage(sender.text() + ' was pressed')

4. 发出信号

自定义信号pyqtSignal():

PyQt5.QtCore.pyqtSignal(types[, name[, revision=0[, arguments=[]]]])
Create one or more overloaded unbound signals as a class attribute.
Parameters:
types: the types that define the C++ signature of the signal. Each type may be a Python type object or a string that is the name of a C++ type. Alternatively each may be a sequence of type arguments. In this case each sequence defines the signature of a different signal overload. The first overload will be the default.
name: the name of the signal. If it is omitted then the name of the class attribute is used. This may only be given as a keyword argument.
revision: the revision of the signal that is exported to QML. This may only be given as a keyword argument.

arguments: the sequence of the names of the signal’s arguments that is exported to QML. This may only be given as a keyword argument.
Return type: an unbound signal

自定义信号的一般流程如下(以示例代码为例):
1. 定义信号 (closeApp = pyqtSignal())
2. 定义槽函数 (self.close)
3. 绑定信号和槽 (self.c.closeApp.connect(self.close))
4. 发射信号 (self.c.closeApp.emit(),这里的信号发射由鼠标按键触发)

from PyQt5.QtCore import pyqtSignal, QObject

class Communicate(QObject):
    closeApp = pyqtSignal()


class Communicate(QMainWindow):

    self.c = Communicate()
    self.c.closeApp.connect(self.close) 

    def mousePressEvent(self, event):
        self.c.closeApp.emit()

综合前面两个功能,得到如下示例:

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


import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
from PyQt5.QtCore import pyqtSignal, QObject


class Communicate(QObject):
    closeApp = pyqtSignal()


class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        btn1 = QPushButton("Button 1", self)
        btn1.move(30, 50)

        btn2 = QPushButton("Button 2", self)
        btn2.move(150, 50)

        btn1.clicked.connect(self.buttonClicked)
        btn2.clicked.connect(self.buttonClicked)

        self.statusBar()

        self.c = Communicate()
        self.c.closeApp.connect(self.close)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Event sender')
        self.show()

    def buttonClicked(self):
        sender = self.sender()
        self.statusBar().showMessage(sender.text() + ' was pressed')

    # mousePressEvent也是一个已经存在的函数
    def mousePressEvent(self, event):
        self.c.closeApp.emit()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

你可能感兴趣的:(PyQt5)