Call QThread.exec () needed in QThread?

I do not call exec() in my code, but timer and QUdpSocket working fine. Is exec() waiting to continue event ?

UPDATE: timer works because I did not call moveToThread(this) in QThread , which meant that QThread is actually still part of the main thread . Regarding QUdpSocket I use polling functions . Therefore, he did not need to work with signals .

TIP: if you need to make init material that requires an event loop in QThread , you can delay call moveToThread until you need signals when the program loads. You also do not need to call it in the constructor (for example, you can call it inside run() ), just copy this QThread to the variable and make a call later / elsewhere using the pointer.

+7
multithreading qt qthread
source share
3 answers

To actually use your thread instead of the QApplication runtime, you must call moveToThread(this) inside the thread constructor. Put the run loop in the protected run() method of your derived QThread class.

A separate thread execution loop prevents clutter of the QApplication loop with non-ui signals and slots and, therefore, a delay in executing the .eg button, making your application lag.

Note. You usually always subclass QThread, see Qt doc for more information.

Edit: Qt doc is incorrect, read this topic http://blog.qt.digia.com/2010/06/17/youre-doing-it-wrong/

+2
source share

Your timer and socket probably use the main event loop that fires when QCoreApplication::exec() called. Although I'm sure there is a good reason to start an event loop in a thread, I can't come up with it.

The QThread documentation says:

Each QThread can have its own event loop. You can start the event loop by calling exec (); you can stop it by calling exit () or quit (). The presence of an event loop in a stream allows you to connect signals from other streams to slots in this stream using a mechanism called queues in a queue. It also allows you to use classes that require an event loop, such as QTimer and QTcpSocket, in a stream. Please note, however, that you cannot use any widget classes in the stream.

Without an event loop, it can generate signals processed by the GUI thread, or another thread containing an event loop. This means that the stream must have an event loop for its slots to be efficient. In the above documentation, some classes, such as QTimer , require a run loop, for which you must call QThread::exec() . Other classes, such as QTCPSocket , have the ability to run with or without an event loop, depending on the functions used. Class documentation should indicate what, if any, requirements they have.

+4
source share

It depends on your programs. Here is an example:

 void MyThread::run(){ Curl * curl = new Curl(); connect( curl, SIGNAL(OnTransfer(QString)), this, SLOTS(OnTransfer(QString)) ); connect( curl, SIGNAL(OnDone()), curl, SLOTS(deleteLater()) ); curl->Download("http://google.com"); exec(); // this is an event loop in this thread, it will wait until you command quit() } void MyThread::OnTransfer(QString data){ qDebug() << data; } 

Without exec (), OnTransfer will never be called. BUT, if you create curl out run with this (suppose the parent of MyThread is the main thread) as the parent:

 MyThread::MyThread(){ curl = new Curl(this); connect( curl, SIGNAL(OnTransfer(QString)), this, SLOTS(OnTransfer(QString)) ); start(); } void MyThread::run(){ curl->Download("http://google.com"); } 

This one will work as you expected. OnTransfer will be called.

0
source share

All Articles