CopperSpice API  1.9.1
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

To create a thread, subclass QThread and reimplement its run() function.

class MyThread : public QThread
{
CS_OBJECT(MyThread)
protected:
void run();
};
void MyThread::run()
{
...
}

Starting a Thread

To start a thread you create an instance of the thread object and then call QThread::start(). You must create the QApplication (or QCoreApplication) object before you can create a QThread. The start() method 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.

The call to QCoreApplication::exec() must always be called from the main() thread, not from another thread. In GUI applications, the main thread is sometimes called the GUI thread because it is the only thread which is allowed to perform GUI operations. Calling GUI methods from a worker thread is undefined behavior.

Creating threads is explained in more detail in the QThread class. documentation.