Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type. The worker thread is implemented as a PyQt thread rather than a Python thread since we want to take advantage of the signals and slots mechanism to communicate with the main application. Class Worker(QThread): def init(self, parent = None): QThread.init(self, parent) self.exiting = False self.size = QSize(0, 0) self.stars = 0.
In this part of the PyQt4 programming tutorial, we will explore events and signals occurring in applications.
Events
All GUI applications are event-driven. Events are generated mainly by the user of an application. But they can be generated by other means as well: e.g. an Internet connection, a window manager, or a timer.When we call the application's exec_()
method, the application enters the main loop. The main loop fetches events and sends them to the objects.
In the event model, there are three participants:
- event source
- event object
- event target
The event source is the object whose state changes. It generates events. The event object (event) encapsulates the state changes in the event source.The event target is the object that wants to be notified. Event source object delegates the task of handling an event to the event target.
PyQt4 has a unique signal and slot mechanism to deal with events. Signals and slots are used for communication between objects. A signal is emitted when a particular event occurs. A slot can be any Python callable.A slot is called when a signal connected to it is emitted.
New API
PyQt4.5 introduced a new style API for working with signals and slots.
This is the old style API.
The new style adheres more to the Python standards.
Signals & Slots
This is a simple example demonstrating signals and slots in PyQt4.
In our example, we display a QtGui.QLCDNumber
and a QtGui.QSlider
. We change the lcd
number by dragging the slider knob.
Here we connect a valueChanged
signal of the slider to thedisplay
slot of the lcd
number.
The sender is an object that sends a signal. The receiver is the object that receives the signal. The slot is the method that reacts to the signal.
Reimplementing event handler
Events in PyQt4 are processed often by reimplementing event handlers.
In our example, we reimplement the keyPressEvent()
event handler.
If we click the Escape button, the application terminates.
Event sender
Sometimes it is convenient to know which widget is the sender of a signal. For this, PyQt4 has the sender()
method.
We have two buttons in our example. In the buttonClicked()
methodwe determine which button we have clicked by calling the sender()
method.
Both buttons are connected to the same slot.
We determine the signal source by calling the sender()
method.In the statusbar of the application, we show the label of the button being pressed.
Emitting signals
Objects created from a QtCore.QObject
can emit signals. In the following example we will see how we can emit custom signals.
We create a new signal called closeApp
. This signal is emitted during a mouse press event. The signal is connected to theclose()
slot of the QtGui.QMainWindow
.
Pyqt5 Tutorial
A signal is created with the QtCore.pyqtSignal()
as a class attributeof the external Communicate
class.
The custom closeApp
signal is connected to the close()
slot of the QtGui.QMainWindow
.
When we click on the window with a mouse pointer, the closeApp
signal is emitted. The application terminates.
Pyqt4 Signals And Slots Tutorial For Beginners
In this part of the PyQt4 tutorial, we have covered signals and slots.