Starting Threads with QThread
A QThread instance represents a thread and provides the means to start() a thread, which will then execute the reimplementation of QThread::run(). The run() implementation is for a thread what the main() entry point is for the application. All code executed in a call stack that starts in the run() function is executed by the new thread, and the thread finishes when the function returns. QThread emits signals to indicate that the thread started or finished executing.
Creating a Thread
- class MyThread : public QThread
- void run();
- void MyThread::run()
Starting a Thread
The function will return immediately and the main thread will continue. The code that appears in the run() reimplementation will then be executed in a separate thread.
Creating threads is explained in more detail in the QThread documentation.
Note that QCoreApplication::exec() must always be called from the main thread (the thread that executes main()), not from a QThread. In GUI applications, the main thread is also called the GUI thread because it's the only thread that is allowed to perform GUI-related operations.