Qt Signal Slot Event Loop
last modified July 16, 2020
But generaly this is bad idea to use Qt:DirectConnection until you really know what is this and there is no other way. Lets explain it more, Each thread created by Qt (including main thread and new threads created by QThread) have Event loop, the event loop is responsible for receiving signals and call aproporiate slots in its thread. Event Loop Work Queues.Use per-thread Qt event loops as work queues Use cross thread signals and slots to assign work Use cross thread signals and slots to return results Avoids locking the work queue.QEventLoop has built-in locks 50.
In this part of the PyQt5 programming tutorial, we explore events and signalsoccurring in applications.
Qt features IQt is a cross-platform toolkit for application development ILargely used and known as a graphical widget library, but Qt is far more than that. IQtCore, event loop with an original signal/slot mechanism, data structures, threads, regular expressions. Signals & Slots Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. Signals and slots are made possible by Qt's meta-object system. An event in Qt is an object which represents something interesting that happened; the main difference between an event and a signal is that events are targeted to a specific object in our application (which decides what to do with that event), while signals are emitted 'in the wild'.
Events in PyQt5
GUI applications are event-driven. Events are generated mainly by theuser of an application. But they can be generated by other means as well; e.g. anInternet connection, a window manager, or a timer.When we call the application's exec_()
method, the application entersthe 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 objectdelegates the task of handling an event to the event target.
PyQt5 has a unique signal and slot mechanism to deal with events.Signals and slots are used for communication between objects. A signalis emitted when a particular event occurs. A slot can be any Python callable.A slot is called when its connected signal is emitted.
PyQt5 signals and slots
This is a simple example demonstrating signals and slots in PyQt5.
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 receiveris the object that receives the signal. The slot is the method thatreacts to the signal.
PyQt5 reimplementing event handler
Events in PyQt5 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 object in PyQt5
Event object is a Python object that contains a number of attributesdescribing the event. Event object is specific to the generated eventtype.
In this example, we display the x and ycoordinates of a mouse pointer in a label widget.
The x and y coordinates are displayd in a QLabel
widget.
Mouse tracking is disabled by default, so the widget only receives mouse moveevents when at least one mouse button is pressed while the mouse is being moved.If mouse tracking is enabled, the widget receives mouse move events evenif no buttons are pressed.
The e
is the event object; it contains data about the eventthat was triggered; in our case, a mouse move event. With the x()
and y()
methods we determine the x and y coordinates ofthe mouse pointer. We build the string and set it to the label widget.
PyQt5 event sender
Sometimes it is convenient to know which widget is the sender of a signal.For this, PyQt5 has the sender
method.
We have two buttons in our example. In the buttonClicked
methodwe determine which button we have clicked by calling thesender()
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 labelof the button being pressed.
PyQt5 emitting signals
Objects created from a QObject
can emit signals.The following example shows how we to emit custom signals.
We create a new signal called closeApp
. This signal isemitted during a mouse press event. The signal is connected to theclose()
slot of the QMainWindow
.
A signal is created with the pyqtSignal()
as a class attributeof the external Communicate
class.
The custom closeApp
signal is connected to the close()
slot of the QMainWindow
.
When we click on the window with a mouse pointer, the closeApp
signalis emitted. The application terminates.
In this part of the PyQt5 tutorial, we have covered signals and slots.
Build complex application behaviours using signals and slots, and override widget event handling with custom events.
As already described, every interaction the user has with a Qt application causes an Event. There are multiple types of event, each representing a difference type of interaction — e.g. mouse or keyboard events.
Events that occur are passed to the event-specific handler on the widget where the interaction occurred. For example, clicking on a widget will cause a QMouseEvent
to be sent to the .mousePressEvent
event handler on the widget. This handler can interrogate the event to find out information, such as what triggered the event and where specifically it occurred.
You can intercept events by subclassing and overriding the handler function on the class, as you would for any other function. You can choose to filter, modify, or ignore events, passing them through to the normal handler for the event by calling the parent class function with super()
.
However, imagine you want to catch an event on 20 different buttons. Subclassing like this now becomes an incredibly tedious way of catching, interpreting and handling these events.
Thankfully Qt offers a neater approach to receiving notification of things happening in your application: Signals.
Signals
Instead of intercepting raw events, signals allow you to 'listen' for notifications of specific occurrences within your application. While these can be similar to events — a click on a button — they can also be more nuanced — updated text in a box. Data can also be sent alongside a signal - so as well as being notified of the updated text you can also receive it.
Qt Signals And Slots Without Event Loop
The receivers of signals are called Slots in Qt terminology. A number of standard slots are provided on Qt classes to allow you to wire together different parts of your application. However, you can also use any Python function as a slot, and therefore receive the message yourself.
Load up a fresh copy of `MyApp_window.py` and save it under a new name for this section. The code is copied below if you don't have it yet.
Basic signals
First, let's look at the signals available for our QMainWindow
. You can find this information in the Qt documentation. Scroll down to the Signals section to see the signals implemented for this class.
Qt 5 Documentation — QMainWindow Signals
As you can see, alongside the two QMainWindow
signals, there are 4 signals inherited from QWidget
and 2 signals inherited from Object
. If you click through to the QWidget
signal documentation you can see a .windowTitleChanged
signal implemented here. Next we'll demonstrate that signal within our application.
Qt Signal Slot Event Loop Tool
Qt 5 Documentation — Widget Signals
The code below gives a few examples of using the windowTitleChanged
signal.
Try commenting out the different signals and seeing the effect on what the slot prints.
We start by creating a function that will behave as a ‘slot’ for our signals.
Qt Signal Slot Event Loop C++
Then we use .connect on the .windowTitleChanged
signal. We pass the function that we want to be called with the signal data. In this case the signal sends a string, containing the new window title.
If we run that, we see that we receive the notification that the window title has changed.
Events
Next, let’s take a quick look at events. Thanks to signals, for most purposes you can happily avoid using events in Qt, but it’s important to understand how they work for when they are necessary.
As an example, we're going to intercept the .contextMenuEvent
on QMainWindow
. This event is fired whenever a context menu is about to be shown, and is passed a single value event
of type QContextMenuEvent
.
To intercept the event, we simply override the object method with our new method of the same name. So in this case we can create a method on our MainWindow
subclass with the name contextMenuEvent
and it will receive all events of this type.
Qt Signal Slot Event Loop 288
If you add the above method to your MainWindow
class and run your program you will discover that right-clicking in your window now displays the message in the print statement.
Sometimes you may wish to intercept an event, yet still trigger the default (parent) event handler. You can do this by calling the event handler on the parent class using super
as normal for Python class methods.
This allows you to propagate events up the object hierarchy, handling only those parts of an event handler that you wish.
However, in Qt there is another type of event hierarchy, constructed around the UI relationships. Widgets that are added to a layout, within another widget, may opt to pass their events to their UI parent. In complex widgets with multiple sub-elements this can allow for delegation of event handling to the containing widget for certain events.
Qt Signal Slot Event Loop Tutorial
However, if you have dealt with an event and do not want it to propagate in this way you can flag this by calling .accept()
on the event.
Alternatively, if you do want it to propagate calling .ignore()
will achieve this.
In this section we've covered signals, slots and events. We've demonstratedsome simple signals, including how to pass less and more data using lambdas.We've created custom signals, and shown how to intercept events, pass onevent handling and use .accept()
and .ignore()
to hide/show eventsto the UI-parent widget. In the next section we will go on to takea look at two common features of the GUI — toolbars and menus.