PyQt5 Quick Start PyQt5 Signal Slot Mechanism

Keywords: Python Qt Lambda Programming

PyQt5 Quick Start (2) PyQt5 Signal Slot Mechanism

1. Introduction of Signal Slot Mechanism

1. Introduction of signal slots

Signal slot is the core mechanism of Qt and also the mechanism of object communication in PyQt programming.In Qt, the QObject object and all controls in PyQt that inherit from QWidget support the slot mechanism.When the signal is transmitted, the connected slot function is automatically executed.In PyQt5, the signal and slot functions are connected by the object.signal.connect() method.
The characteristics of the signal slot are as follows:
(1) One signal can connect multiple slots
(2) One signal may connect another signal
(3) Signal parameters can be of any Python type
(4) One slot can monitor multiple signals
(5) The connection between signal and slot can be synchronous or asynchronous.
(6) Signal-slot connections can cross threads
(7) Signal can be disconnected
When writing a class, the signal and slot of the class are defined first, and the signal and slot are connected in the class to realize data transmission between objects.When an event or state changes, a signal is issued that triggers the execution of the slot function associated with the event or signal.The signal slot mechanism is illustrated as follows:

2. Define the signal

PyQt's built-in signals are automatically defined, using the PyQt5.QtCore.pyqtSignal function to create a signal for a QObject object, and using the pyqtSignal function to define the signal as a property of a class.

class pyqtSignal:
    def __init__(self, *types, name: str = ...) -> None: ...

The types parameter denotes the type of parameters that define the signal, the name parameter denotes the name of the signal, and the class's attribute name is used by default.
Use the pyqtSignal function to create one or more overloaded unbound signals as attributes of the class, signals can only be defined in subclasses of QObject.Signals must be defined at class creation time and cannot be added dynamically as attributes of a class after it is created.When a signal is defined using the pyqtSignal function, it can pass multiple parameters and specify the type of the signal transfer parameters, which are standard Python data types, including strings, dates, Boolean types, numbers, lists, dictionaries, tuples.

from PyQt5.QtCore import pyqtSignal, QObject

class StandardItem(QObject):
    # Defines a signal with two parameters, STR and dataChanged
    data_changed = pyqtSignal(str, str, name="dataChanged")

    # Update information, send signal
    def update(self):
        self.dataChanged.emit("old status", "new status")

3. Operational signal

The connect function binds the signal to the slot function, the disconnect function unbinds the signal to the slot function, and the emit function emits the signal.
QObject.signal.connect(self, slot, type=None, no_receiver_check=False)
Establish the connection of signal to slot function, type is connection type.
QObject.signal.disconnect(self, slot=None)
Disconnect signal from slot
emit(self, *args)
Send signal, args is variable parameter.

import sys
from PyQt5.QtCore import pyqtSignal, QObject, QCoreApplication

class StandardItem(QObject):
    # Defines a signal with two parameters, STR and dataChanged
    data_changed = pyqtSignal(str, str, name="dataChanged")

    # Update information, send signal
    def update(self):
        self.dataChanged.emit("old status", "new status")

    # Define Slot Function
    def onDataChanged(self, old, new):
        print(old)
        print(new)

if __name__ == "__main__":
    app = QCoreApplication(sys.argv)

    item = StandardItem()
    item.dataChanged.connect(item.onDataChanged)
    item.update()

    sys.exit(app.exec_())

# OUTPUT:
# old status
# new status

2. Signal and Slot Application

1. Built-in signal and slot function

The built-in signal is the signal automatically defined by the QObject object, and the built-in slot function is the slot function automatically defined by the QObject object. The built-in signal of the QObject object can be connected to the slot function of the QObject object through the QObject.signal.connect function.

2. Built-in signal and custom slot function

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("MainWindow Demo")
        self.resize(800, 600)

        button = QPushButton("close", self)
        # Connect built-in signal to custom slot
        button.clicked.connect(self.onClose)

    # Custom Slot Function
    def onClose(self):
        self.close()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

When a button is clicked, the button's built-in clicked signal is triggered to execute the bound custom slot function onClose.

3. Custom signal and built-in slot function

import sys
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton

class MainWindow(QWidget):
    closeSignal = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("MainWindow Demo")
        self.resize(800, 600)

        button = QPushButton("close", self)
        # Connect built-in signal to custom slot
        button.clicked.connect(self.onClose)
        # Connect custom signal closeSignal with built-in slot function close
        self.closeSignal.connect(self.close)

    # Custom Slot Function
    def onClose(self):
        # Send Custom Signal
        self.closeSignal.emit()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

By connecting the built-in signal clicked to the custom slot function onClose, a custom signal closeSignal is sent within the custom slot function onClose, and the custom signal closeSignal is connected to the built-in slot function close.

4. Custom Signal and Custom Slot Function

import sys
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton

class MainWindow(QWidget):
    closeSignal = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("MainWindow Demo")
        self.resize(800, 600)

        button = QPushButton("close", self)
        # Connect built-in signal to custom slot
        button.clicked.connect(self.onClicked)
        # Connect custom signal closeSignal with built-in slot function close
        self.closeSignal.connect(self.onClose)

    # Custom Slot Function
    def onClicked(self):
        # Send Custom Signal
        self.closeSignal.emit()

    # Custom Slot Function
    def onClose(self):
        self.close()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

3. Advances in signal slot applications

1. Custom Signal Slot

Signal objects are usually defined through class variables, and custom signals are defined before u init_u functions.

class TestObject(QObject):
    # Define signal without parameters
    noParametersSignal = pyqtSignal()
    # Signal defining a parameter of type int
    oneParameterSignal = pyqtSignal(int)
    # Signal defining the overloaded version of a parameter, which can be of type int or str
    oneParameterOverloadSignal = pyqtSignal([int], [str])
    # Signal defining the overloaded version of two parameters of type int,str or int,int
    twoParametersOverloadSignal = pyqtSignal([int, str], [int, int])
    # Signal defining a list parameter type
    oneParameterSignalList = pyqtSignal(list)
    # Signal defining a dict parameter type
    oneParameterSignalDict = pyqtSignal(dict)

The slot function definition of a class is the same as the general method definition of a class.

class TestObject(QObject):
    # Define signal without parameters
    noParametersSignal = pyqtSignal()
    # Signal defining a parameter of type int
    oneParameterSignal = pyqtSignal(int)
    # Signal defining the overloaded version of a parameter, which can be of type int or str
    oneParameterOverloadSignal = pyqtSignal([int], [str])
    # Signal defining the overloaded version of two parameters of type int,str or int,int
    twoParametersOverloadSignal = pyqtSignal([int, str], [int, int])

    # Define a slot function without parameters
    def onNoParameterSlot(self):
        pass

    # Defines a slot function with an integer nIndex
    def onOneParameterSlot(self, nIndex):
        pass

    # Defines a slot function with two parameters, an integer nIndex and a string sStatus
    def onTwoParametersSlot(self, nIndex, sStatus):
        pass

Signal and slot function can belong to the same QObject object or different QObject objects by connect ing signal and slot function.

test = TestObject()
test.noParametersSignal.connect(test.onNoParameterSlot)
test.oneParameterSignal.connect(test.onOneParameterSlot)
test.twoParametersOverloadSignal.connect(test.onTwoParametersSlot)

The signal is sent by the emit method.

def update(self):
    self.noParametersSignal.emit()
    self.oneParameterSignal.emit(100)
    self.oneParameterOverloadSignal("Hello, PyQt5")
    self.twoParametersOverloadSignal(100, "Hello, PyQt5")

2. Signal slot transfer custom parameters

The number of parameters emitted by the signal in Qt must be greater than or equal to the number of parameters of the slot function. PyQt uses custom parameter transfer to solve the problem that there are more parameters in the slot function than in the signal.A Lambda expression or a functools partial function can be used to pass custom parameters to the slot function, which can be of any Python type.

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QHBoxLayout
from functools import partial

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        button1 = QPushButton("Button1", self)
        button2 = QPushButton("Button2", self)

        layout = QHBoxLayout()
        layout.addWidget(button1)
        layout.addWidget(button2)
        self.setLayout(layout)

        self.setWindowTitle("MainWindow Demo")
        self.resize(800, 600)

        # lambda
        button1.clicked.connect(lambda: self.onButtonClicked(1))
        button2.clicked.connect(lambda: self.onButtonClicked(2))
        # partial
        button1.clicked.connect(partial(self.onButtonClicked, 1))
        button2.clicked.connect(partial(self.onButtonClicked, 2))

    # Custom Slot Function
    def onButtonClicked(self, n):
       print("Button {0} is Clicked".format(n))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

3. Signal slots and ornaments

Signal and slot functions can be defined in PyQt using a Python decorator as follows:

@PyQt5.QtCore.pyqtSlot(bool)
def on_Sender Object Name_Launch signal name(self, parameter):
    pass

The sender object name is the object name set for the QObject object using setObjectName, and the connectSlotsByName function connected to the slot function by the signal name is as follows:
QtCore.QMetaObject.connectSlotsByName(self, QObject)
ConneSlotsByName is used to connect certain signals of QObject descendant objects to corresponding slot functions of certain QObject objects by name.

import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QHBoxLayout

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        button1 = QPushButton("Button1", self)
        button1.setObjectName("Button1")
        button2 = QPushButton("Button2", self)
        button2.setObjectName("Button2")

        layout = QHBoxLayout()
        layout.addWidget(button1)
        layout.addWidget(button2)
        self.setLayout(layout)

        self.setWindowTitle("MainWindow Demo")
        self.resize(800, 600)

        QtCore.QMetaObject.connectSlotsByName(self)

    @QtCore.pyqtSlot()
    def on_Button1_clicked(self):
        print("Button1 is clicked")

    @QtCore.pyqtSlot()
    def on_Button2_clicked(self):
        print("Button2 is clicked")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

4. Event handling mechanism

1. The difference between event mechanism and signal slot mechanism

PyQt provides a high-level signal slot mechanism and a low-level event processing mechanism for event processing, which is an advanced encapsulation of event processing mechanisms.When using a control, you don't need to consider the event handling mechanism, you only need to care about the signal slot. For a custom derived control, you must consider the event handling mechanism and re-implement the corresponding event handling function according to the behavior requirements of the control.

2. Method of event handling

PyQt provides five event handling and filtering methods:
(1) Re-implement event handling functions
Common event handling functions such as paintEvent, mouseMoveEvent, mousePressEvent, mouseReleaseEvent, and so on.
(2) Re-implement the QObject.event event distribution function
When adding new events, you need to re-implement the QObject.event method and add distribution routes for new events.
(3) Install event filters
If the installEventFilter method is called on a QObject object, an event filter is installed on the QObject object.All events of the QObject object are first passed to the event filter eventFilter function, in which certain events can be discarded or modified, custom event handling mechanisms are used for events of interest, and default event handling mechanisms are used for other events.Event filtering mechanism filters all events of QObject, so more events to filter can affect program performance.
(4) Install event filters in QApplication
Installing event filters on QApplication objects filters all events on all QObject objects and gets events first, which are sent to QApplication's event filters before sending them to any other event filters.
(5) Notfy method for QApplication
PyQt distributes events using the notify method of the QApplication object. The only way to capture events before any event filter is to re-implement the notify method of the QApplication.

3. Examples of event handling

The QDialog dialog automatically exits when the ESC key is pressed, and the ESC key is pressed using event handling and filtering.
(1) Re-implement event handling functions

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt

class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

    # Re-implement keyPressEvent
    def keyPressEvent(self, event):
        if event.key() != Qt.Key_Escape:
            QDialog.keyPressEvent(self, event)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    dialog = Dialog()
    dialog.exec_()

    sys.exit(app.exec_())

(2) Re-implement the event function

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeyEvent

class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

    # Re-implement keyPressEvent
    def event(self, event):
        if event.type() == QKeyEvent.KeyPress and event.key() == Qt.Key_Escape:
            return True
        else:
            return QDialog.event(self, event)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    dialog = Dialog()
    dialog.exec_()

    sys.exit(app.exec_())

(3) QObject Installation Event Filter

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeyEvent

class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.installEventFilter(self)

    def eventFilter(self, watched, event):
        if event.type() == QKeyEvent.KeyPress and event.key() == Qt.Key_Escape:
            return True
        else:
            return QDialog.eventFilter(self, watched, event)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    dialog = Dialog()
    dialog.exec_()

    sys.exit(app.exec_())

(4) QApplication Installation Event Filter

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeyEvent

class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

    def eventFilter(self, watched, event):
        if event.type() == QKeyEvent.KeyPress and event.key() == Qt.Key_Escape:
            return True
        else:
            return QDialog.eventFilter(self, watched, event)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    dialog = Dialog()
    app.installEventFilter(dialog)
    dialog.exec_()

    sys.exit(app.exec_())

Posted by globalinsites on Sun, 21 Jul 2019 09:24:23 -0700