In order to be able to start working on new tasks, change the number of threads to use etc. You need a communication channel to 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. QObject: thread affinity Thread safety in Qt p.31 What about QObject? QObject itself is thread-aware. Every QObject instance holds a reference to the thread it was created into (QObject::thread) We say that the object lives in, or has affinity with that thread We can move an instance to another thread by calling QObject::moveToThread(QThread.).

  • Qt takes care of copying the arguments, before they cross the thread boundaries – and everything is fine. By the way, it doesn’t matter whether we specify the argument in a connect call as const Copy& or Copy. Qt normalises the type to Copy any way. This normalisation does not imply, however, that arguments of signals and slots are always.
  • QObject::killTimer: Timers cannot be stopped from another thread Stopped worker process in Thread 0x15b8 After closing the window: QObject::QObject: Timers cannot be stopped from another thread @mrjj case would have a signal from the simulator, but I just get the signal from pushing the stop button.

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 :

  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:

  • 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.

Program

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

  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.

EnArBgDeElEsFaFiFrHiHuItJaKnKoMsNlPlPtRuSqThTrUkZh

Threads in an operating system are a very simple thing. Write a function, maybe bundle it with some data and push it onto a newly created thread. Use a mutex or other method to safely communicate with the thread if necessary. Whether it are Win32, POSIX or other threads, they all basically work the same and are quite fool-proof.

Those who have discovered the joys of the Qt framework may assume that threads in Qt are just like this, and they would be right. However, there are several different ways to use threads in Qt, and it might not be obvious which approach to choose. The article, Multithreading Technologies in Qt, compares the different approaches.

The rest of this article demonstrates one of these methods: QThread + a worker QObject. This method is intended for use cases which involve event-driven programming and signals + slots across threads.

Usage with Worker class

Qt invoke method another thread

The main thing in this example to keep in mind when using a QThread is that it's not a thread. It's a wrapper around a thread object. This wrapper provides the signals, slots and methods to easily use the thread object within a Qt project. To use it, prepare a QObject subclass with all your desired functionality in it. Then create a new QThread instance, push the QObject onto it using moveToThread(QThread*) of the QObject instance and call start() on the QThread instance. That's all. You set up the proper signal/slot connections to make it quit properly and such, and that's all.

Declare Worker class

For a basic example, check this class declaration for the Worker class:

class Worker : public QObject {

public:

public slots:

signals:

private:

};

We add at least one public slot which will be used to trigger the instance and make it start processing data once the thread has started. Now, let's see what the implementation for this basic class looks like.

Worker::Worker() { // Constructor

}

Worker::~Worker() { // Destructor

}

Qt Invoke Slot Another Thread Set

void Worker::process() { // Process. Start processing data.

}

While this Worker class doesn't do anything special, it nevertheless contains all the required elements. It starts processing when its main function, in this case process(), is called and when it is done it emits the signal finished() which will then be used to trigger the shutdown of the QThread instance it is contained in.

By the way, one extremely important thing to note here is that you should NEVER allocate heap objects (using new) in the constructor of the QObject class as this allocation is then performed on the main thread and not on the new QThread instance, meaning that the newly created object is then owned by the main thread and not the QThread instance. This will make your code fail to work. Instead, allocate such resources in the main function slot such as process() in this case as when that is called the object will be on the new thread instance and thus it will own the resource.

Create a new Worker instance

Now, let's see how to use this new construction by creating a new Worker instance and putting it on a QThread instance:

QThread* thread = new QThread;Worker* worker = new Worker();worker->moveToThread(thread);connect(worker, SIGNAL (error(QString)), this, SLOT (errorString(QString)));connect(thread, SIGNAL (started()), worker, SLOT (process()));connect(worker, SIGNAL (finished()), thread, SLOT (quit()));connect(worker, SIGNAL (finished()), worker, SLOT (deleteLater()));connect(thread, SIGNAL (finished()), thread, SLOT (deleteLater()));thread->start();

The connect() series here is the most crucial part. The first connect() line hooks up the error message signal from the worker to an error processing function in the main thread. The second connects the thread's started() signal to the processing() slot in the worker, causing it to start.

Then the clean-up: when the worker instance emits finished(), as we did in the example, it will signal the thread to quit, i.e. shut down. We then mark the worker instance using the same finished() signal for deletion. Finally, to prevent nasty crashes because the thread hasn't fully shut down yet when it is deleted, we connect the finished() of the thread (not the worker!) to its own deleteLater() slot. This will cause the thread to be deleted only after it has fully shut down.

Qt Call Method From Another Thread


Qt Invoke Slot Another Thread Tool

External Links

Qt Invoke Slot Another Thread Pattern

  1. Maya Posch's blog, http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
  2. Qt Blog on subclassing QThread is wrong, [1]
  3. Woboq Blog on subclassing QThread is not always wrong, [2]

Qt Invoke Slot Another Thread Holder

Retrieved from 'https://wiki.qt.io/index.php?title=QThreads_general_usage&oldid=18828'
Coments are closed
Scroll to top