In the official Qt Documentation:
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 thread
Now, in a qt project i've tried the following code:
QThread* thread = new QThread;
DetectList *list = new DetectList;
list->moveToThread(thread);
connect(thread, SIGNAL(started()), list, SLOT(process()));
thread->start();
Where DetectList is a class derived by QWidget. Why the code compile and run? DetectList doesn't must be run only in the main thread?
Like Laszlo Papp point out you are receiving a warring and
moveToThread
have no effect. Warring will say:QObject::moveToThread: Widgets cannot be moved to a new thread
See source code of moveToThread.
I recommend you to describe what are you exacly trying to do and why you nead threads. I'm pretty sure there is better solution (like Qt Concurrent).
The program will compile and run because from a
C++
prosepctive it is syntactically correct.What
Qt
documentation says is that it is not correct to have GUI related code running in a different thread than the main thread, and if that happens then the application will probably crash in runtime.In your previous code for example if the
DetectList
was object was interacting with some GUI element then your program would crash: