Qt Signal Slot Queued Connection

Posted By admin On 31/03/22
  • PyQt Tutorial

The Signal/Slot Editor. The signal and slot used in a connection can be changed after it has been set up. When a connection is configured, it becomes visible in Qt Designer's signal and slot editor where it can be further edited. You can also edit signal/slot connections by double-clicking on the connection path or one of its labels to display. With queued connections, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. Call qRegisterMetaType to register the data type before you establish the connection. Emitting a signal to a direct connection within the same thread will execute the slot immediately, just like a simple function call. Emitting a signal to a queued connection within the same thread will enqueue the call into the threads event loop, thus the execution will always happen delayed. QObject based class has a queued connection to itself.

  • PyQt Useful Resources
  • Selected Reading

Unlike a console mode application, which is executed in a sequential manner, a GUI based application is event driven. Functions or methods are executed in response to user’s actions like clicking on a button, selecting an item from a collection or a mouse click etc., called events.

Widgets used to build the GUI interface act as the source of such events. Each PyQt widget, which is derived from QObject class, is designed to emit ‘signal’ in response to one or more events. The signal on its own does not perform any action. Instead, it is ‘connected’ to a ‘slot’. The slot can be any callable Python function.

In PyQt, connection between a signal and a slot can be achieved in different ways. Following are most commonly used techniques −

A more convenient way to call a slot_function, when a signal is emitted by a widget is as follows −

A developer can choose to connect to a signal by creating a function (a 'slot') and calling the connect function to relate the signal to the slot. Qt's signals and slots mechanism does not require classes to have knowledge of each other, which makes it much easier to develop highly reusable classes.

Suppose if a function is to be called when a button is clicked. Here, the clicked signal is to be connected to a callable function. It can be achieved in any of the following two techniques −

Queued

or

Example

In the following example, two QPushButton objects (b1 and b2) are added in QDialog window. We want to call functions b1_clicked() and b2_clicked() on clicking b1 and b2 respectively.

When b1 is clicked, the clicked() signal is connected to b1_clicked() function

When b2 is clicked, the clicked() signal is connected to b2_clicked() function

Qt signal slot queued connection download

Example

Qt Signal Slot Queuedconnection

The above code produces the following output −

Qt Signal Slot Queued Connections

Output