|
[Thread Support in Qt]
[Next: Synchronizing Threads]
Запускаем потоки с помощью 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.
Создание потока
Для создания потока создайте подкласс QThread и заново реализуйте его функцию run(). Например:
class MyThread : public QThread
{
Q_OBJECT
protected:
void run();
};
void MyThread::run()
{
...
}
Starting a Thread
Затем создайте экземпляр объекта вашего потокового класса и вызовите QThread::start(). Note that you must create the QApplication (or QCoreApplication) object before you can create a QThread.
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.
Обратите внимание на то, что QCoreApplication::exec() всегда должна вызываться из главного потока (потока, в котором выполняется main()), а не из QThread. В приложениях с графическим пользовательским интерфейсом (ГПИ) главный поток также называется потоком GUI, потому что только ему разрешается выполнять какие-либо действия, связанные с ГПИ.
[Thread Support in Qt]
[Next: Synchronizing Threads]
| Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) |
Торговые марки |
Qt 4.6.4 |
|