I am not calling exec()
in my code, but the timer
and QUdpSocket
is working fine. Is exec()
used to wait for an event
to continue?
UPDATE: the timer
was working, because I had not called moveToThread(this)
on the QThread
, which meant the QThread
was actually still part of the main thread
. As for QUdpSocket
well I using the polling functions
. So it did not need to work with signals
.
TIP: if you need to do init
stuff, that requires an event loop
in your QThread
, you can delay
calling moveToThread
until you don't need the signals
anymore, that is practical when the program is loading. You also don't need to call it in the constructor (you could call it inside run()
for example), just copy the this QThread
pointer to a variable and make the call
later/elsewhere using the pointer.
In order to actually use your thread, instead of the QApplication execution loop, you have to call moveToThread(this)
within the thread constructor AND place an execution loop within the protected run()
method of your QThread derived class.
Seperate threads execution loop prevents the QApplication loop getting cluttered with non-ui related signals and slots, and therefore delaying for .e.g button click's slot execution, making your application "lag".
Note: Normally you always subclass QThread, see Qt doc for more information
Edit: Qt doc is wrong, read this thread https://blog.qt.io/blog/2010/06/17/youre-doing-it-wrong/