Qt Run Slot In Another Thread

Posted By admin On 02/04/22

Hi,
Let's say i have a MainWindow and a button on it and on it's button click i want to run a few functions each one in a separate thread. how can i achieve this?

@void MainWindow::on_pushButton_clicked()
{
function1(); // Thread 1
function2(); // Thread 2
function3(); // Thread 3
}@
these functions are not related to each other so they don't need to be synced together.

Creating a class and moving it to a thread in a different QThread prevents QWebChannel's queued interaction. To Reproduce: Implement a new class which contains at least 1 public slot (void return) Create an instance of custom class derived from QObject myClass; Create an instance of QThread myClassThread. Qt's event systemis very useful for inter-thread communication. Every thread may have its own event loop. To call a slot (or any invokablemethod) in another thread, place that call in the target thread's event loop. Very easy, and wil run each function in a pool thread, Look for QtConcurrent in dicumentation. If you find managing QThreads and low-level primitives like mutexes or semaphores too complex, Qt Concurrent namespace is what you are looking for. It includes classes which allow more high-level thread management. Let's look at Concurrent Run. QtConcurrent::run allows to run function in a new thread. When would you like to use it? Then connect the main thread and algorithm thread by signal and slots. Namely, you can now emit a signal in one thread and receive it in a slot in a different thread. The following is a simple example, in which a second-timer is wrapped in a QThread and a QWidget in main thread can start and stop the timer in any time.

i already tried below code as an example however no luck :-(
@
void MainWindow::on_pushButton_clicked()
{
QThread thread;
QThread::currentThread()->setObjectName('MainThread');
thread.setObjectName('CustomThread');
MyClass obj;
obj.moveToThread(&thread);
thread.start();
if(thread.isRunning())
{
obj.sayHello();
thread.quit();
}
}

void MyClass::sayHello()
{
QString s1 = QThread::currentThread()->objectName();
}
@
i get 'MainThread' in s1.

Qt - Passing custom objects among threads

Details
Category: Programming
Written by Nandan Banerjee
Hits: 13055

Communication between threads in a qt program is essentially done by using signals/slots. This is by far one of the most easiest and stable mode of communication amongst threads of a program.

Qt Run Slot In Another Threaded

Run

For example, let us suppose that one thread needs to send an integer value to another thread. All the programmer needs to do is simply create a dispatch signal with two arguments, the thread id and the integer value. Thus, it can be achieved by this simple line –

Qt Run Slot In Another Thread Set

Similarly, a receive slot with the same arguments needs to be created to receive the signal.

Now, this will work very nicely when one is dealing with the predefined primitive or the qt data types. This is because they are already registered and hence it is not a problem for qt to recognise those data types.

The problem arises when one wants to pass a custom data type (any class or structure that has a public default constructor, a public copy constructor, and a public destructor can be registered). Then, the user defined class or a class defined in a library not part of qt can be passed using signals/slots after registering.

The qRegisterMetaType() function is used to make the type available to non-template based functions, like the queued signal and slot connections.

This is done in the following way –

where name can be any custom data type.

For example, let us take a program which will capture the image from the webcam and display it in a QLabel on the GUI. To achieve this, two approaches can be taken. Run the camera grabbing function in the main UI thread or in a different thread and reducing the work of the UI thread significantly. If it is run in the main UI thread, then the chances of the UI thread not responding is very high. Therefore, it is always desirable make a separate thread and use it instead to run the camera grabbing function.

Run

In this tutorial, we will use the openCV library to grab an image from the webcam and use the signal/slot mechanism to send the image (IplImage type) to the UI thread.

After creating a new qtGUI project, a new class is created (say “webcamThread”) with QThread as its parent class. A run() function is defined and a new signal with the image as the argument is defined.

In the MainWindow file, a slot is defined to handle the signal from the webcamThread. This image is then converted to the QImage format and then displayed in the QLabel. So, a smooth and pleasant webcam feed can be achieved using this.

Qt code for the webcam feed -

The openCV library needs to be present in the system and the paths should be appropriately set.

Qt Run Slot In Another Thread Pattern

// webcamthread.h

// webcamthread.cpp

Now the code for the MainWindow. The signals are connected with the slots and the event handlers are defined.

Qt Run Slot In Another Thread Size

// mainwindow.h

// mainwindow.cpp

In the UI editor, two buttons (Start and Stop) and a label of size 320 by 240 need to be created. Then, just compile and run. So, it can be seen that objects of the class “IplImage” from the openCV library can be easily passed between the threads just by registering the class.