Qt Run Slot In Another Thread

broken image


Based on one of the questions on the forum, I wrote an example on using QThread in PyQt5, as well as using the moveToThread method to move the class object of the inherited QObject to another thread.

In this example, a certain algorithm is executed, which returns the text through the signal, as well as the color of the text to the main GUI. This data is added to the QTextBrowser with a color setting.

The program will look as follows

Introduction

There are two main approaches for using QThread in Qt :

Slot
  1. Create a new class that inherits from QThread and override the run method
  2. Create a new class that inherits from QObject , write a run method that will execute some code, and transfer the instance of this class to another thread using the moveToThread method

The first method is recommended to be used only if you really need to override the stream class in order to create special functionality in the stream class. If you need to execute some code in another thread, then for this you need to create a separate class that will be transferred to another thread using the moveToThread method.

Also, I want to note right away that you cannot transfer GUI objects to other threads. Qt programs have two kinds of threads:

Qt Run Slot In Another Thread

Very easy, and wil run each function in a pool thread, Look for QtConcurrent in dicumentation. The Thread::doWork function lives in another thread than Object::started, which has been created in run. If the Thread has been instantiated in the UI Thread, this is where doWork would have belonged. This system is powerful but complex. To make things more simple, Qt favors the worker model.

  • Main stream. GUI thread
  • Workflows. Worker threads
Thread

All GUI objects should create and work only in a GUI thread, while various other algorithms can be executed in worker threads.

Qt Run Slot In Another Thread

As mentioned, each program has one thread when it is started. This thread is called the 'main thread' (also known as the 'GUI thread' in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a 'worker thread' because it is used to offload processing work from the main thread.

Thread
  1. Create a new class that inherits from QThread and override the run method
  2. Create a new class that inherits from QObject , write a run method that will execute some code, and transfer the instance of this class to another thread using the moveToThread method

The first method is recommended to be used only if you really need to override the stream class in order to create special functionality in the stream class. If you need to execute some code in another thread, then for this you need to create a separate class that will be transferred to another thread using the moveToThread method.

Also, I want to note right away that you cannot transfer GUI objects to other threads. Qt programs have two kinds of threads:

Very easy, and wil run each function in a pool thread, Look for QtConcurrent in dicumentation. The Thread::doWork function lives in another thread than Object::started, which has been created in run. If the Thread has been instantiated in the UI Thread, this is where doWork would have belonged. This system is powerful but complex. To make things more simple, Qt favors the worker model.

  • Main stream. GUI thread
  • Workflows. Worker threads

All GUI objects should create and work only in a GUI thread, while various other algorithms can be executed in worker threads.

As mentioned, each program has one thread when it is started. This thread is called the 'main thread' (also known as the 'GUI thread' in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a 'worker thread' because it is used to offload processing work from the main thread.

That is, if, even if something works for you in another thread, it will be only an accident that will sooner or later make itself felt. And your program will stop working. Never pass other GUI objects to other threads.

Qt Run Slot In Another Threaded

Program

Now consider the code of our program. Please note that the algorithm of actions will be as follows

Qt Run Slot In Another Thread Set

  1. We write a class that inherits from QObject and has a run method for executing code in another thread
  2. In the window constructor, create a stream object
  3. In the window constructor, create an object that will be transferred to another thread
  4. Transfer the object to another stream
  5. We connect signals and slots
  6. Run the thread

It is advisable to perform all the initialization of the object and stream in this sequence, if you do not already have sufficient experience.
However, then you would not read this article.
If you swap steps 4 and 5, then you first connect the signals and slots, and then transfer the object to another stream, which will break the signal / slot connection. The application window will stop working. Or the application may just crash.





broken image