Hardware and Software Environment
- OS X EI Capitan
- Python 3.5.1
- PyQt 5.5.1
Objective
Signals (Signal) and slots (slots) are the mechanisms of communication between objects in QT programming. In Qt, the signal and slot mechanisms are supported for each Qobject object, including all controls in the pyqt that inherit from Qwidget (because they are all qobject sub-objects).
Signal and slot bindings
Or the previous FIRSTPYQT5 project as an example,
self.pushButton.clicked.connect(self.firtPyQt5_button_click)
The above code shows that when the button pushbutton (Qpushbutton is a subclass of Qwidget) is clicked (releasing the clicked Signal), the slot (Firtpyqt5_button_click) is executed.
The same signal binds multiple slots
PYQT5 allows a signal to bind multiple slots at the same time, such as
self.pushButton.clicked.connect(self.firtPyQt5_button_click)self.pushButton.clicked.connect(self.firtPyQt5_button_click_2)
From the execution results, the slots are executed in the same order that they were bound, that is, binding first. In addition, for multiple signal binding the same slot, it is easier to understand, not much to say
One signal binds another signal
There is also a special situation is signal and signal binding, here is assumed to have a Qpushbutton pushButton1, we let its clicked signal and pushbutton signal clicked binding, as follows
self.pushButton.clicked.connect(self.firtPyQt5_button_click)self.pushButton1.clicked.connect(self.pushButton.clicked)
Signal's Unbind
PYQT5 provides a disconnect method to unbind
self.pushButton1.clicked.disconnect(self.pushButton.clicked)self.pushButton.clicked.disconnect(self.firtPyQt5_button_click_2)
Custom Signal
PYQT5 provides a qtcore.pyqtsignal function to produce a signal, followed by a familiar binding operation, to see an example is clear
class EmitSignal(QWidget): def __init__(self): super(EmitSignal,self).__init__() self.customSignal = pyqtSignal() def emitCustomSignal(self): self.customSignal.emit()
PYQT5 Series Tutorials (iv) Signals and slots