Qt Signals Slots Threads Example

  • PyQt Tutorial
  • PyQt Useful Resources
  • Selected Reading

Qt signals slots threads example is highly popular and great fun because you can play at any qt signals slots threads example time of the day or night, on your pc, tablet or mobile. There are always hundreds of slots and many other games available and you don't even need to change out of your pajamas to enjoy them. Feb 20, 2013  The Trolls created a new way to connect signals to slots such that signals can actually cross thread boundaries. I can now emit a signal in one thread and receive it in a slot in a different thread. This is hugely useful when you want to, for example, integrate a blocking-happy library into your application. Here’s what I’m talking about in. Qt provides the signals and slots framework which allows you to do just that and is thread-safe, allowing safe communication directly from running threads to your GUI frontend. Signals allow you to.emit values, which are then picked up elsewhere in your code by slot functions which have been linked with.connect.


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 −

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 −

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

Example

Qt Thread Example

The above code produces the following output −

Output

Qt does not provide support for using the Signals & Slots mechanism in combination with C++ templates. See: https://doc.qt.io/qt-5/why-moc.html

However: It is actually possible to do this, when one is willing to keep track of the things normally handledby the QObject::connect() function and 'call' slots with QMetaObject::invokeMethod().

Qt Signal Slot Parameter

Doing this has some nice side effects like not having to call qRegisterMetaType() or using the Q_DECLARE_METATYPE macro.

For a more detailed explanation take a look at the documentation of the invokeInContext() function in Magic.h

Furthermore does Qt provide mechanisms for threading, as being described here: https://doc.qt.io/qt-5/threads-technologies.html#choosing-an-appropriate-approach

The case 'Have an object living in another thread that can perform different tasks upon request and/or can receive new data to work with'

has the proposed solution 'Subclass a QObject to create a worker. Instantiate this worker object and a QThread. Move the worker to the new thread. Send commands or data to the worker object over queued signal-slot connections'

which implies not being able to use C++ templates, when relying solely on the native Signals & Slots mechanism.

But since I am lazy and don't like Copy & Paste, this framework provides a solution for handling that case in combination with C++ templates.

The main idea is to have 'Tasks' and 'Results', which are being described by template parameters 'T' and 'R' in this framework.

A Worker, which is running in his own thread, receives a task and responds with a result.

In general do you need to inherit from the Processor template class, the Worker template classand implement at least the pure virtual methods to create the functionality you want and give instances of thosenew classes to a Controller. That's it.

In order to be able to start working on new tasks, change the number of threads to use etc. you need a communication channelto the instance of the Processor.

You may use Qt Signals & Slots or use the invokeInContext() function Signals & Slots mechanism of this framework.The first example in src/examples/one/ covers all of that.

The second example in src/examples/two/ focuses only on the Signals & Slots system of this framework.

A third example in src/examples/three/ shows usage of the threading architecture + Signals and Slots of this frameworkcompletely without using Qt Signals and Slots.

Qt Signals Slots Threads Example

Everything you need is documented in the CMakeLists.txt.

If you don't know what to do with such a file, you should use a search engine to find out.

Use the Doxyfile to generate the documentation via Doxygen.

Qt Signal Slot Example

Look into the src/examples/ folder.