Is there a way to prevent a thread created inside a slot to be destroyed, after the slot is finished ?
<widget.cpp>
...
void Widget::<slot_name>()
{
...
ThreadTask Watcher; //ThreadTask is defined in header file
QThread WatcherThread;
Watcher.moveToThread(&WatcherThread);
QObject::connect ...
WatcherThread.start();
...
}
I know I could declare the thread outside the slot and only start it from it, but the thing is, I would like to create another thread if the same slot is triggered again.
Use dynamic allocation :
QThread* WatcherThread = new Qthread();
Notice that you must delete it when the thread's execution has finished in order to avoid a memory leak.
Qt could automatically delete the thread when it finishes if you use the following code:
connect(WatcherThread , SIGNAL(finished()),
WatcherThread , SLOT(deleteLater()));