QThread
QThread
| Header: | #include <QThread> |
| qmake: | QT += core |
| Inherits: | QObject |
Public Types
| enum | Priority { IdlePriority, LowestPriority, LowPriority, NormalPriority, ..., InheritPriority } |
Public Functions
| QThread(QObject *parent = nullptr) | |
| virtual | ~QThread() |
| QAbstractEventDispatcher * | eventDispatcher() const |
| void | exit(int returnCode = 0) |
| bool | isFinished() const |
| bool | isInterruptionRequested() const |
| bool | isRunning() const |
| int | loopLevel() const |
| QThread::Priority | priority() const |
| void | requestInterruption() |
| void | setEventDispatcher(QAbstractEventDispatcher *eventDispatcher) |
| void | setPriority(QThread::Priority priority) |
| void | setStackSize(uint stackSize) |
| uint | stackSize() const |
| bool | wait(unsigned long time = ULONG_MAX) |
Reimplemented Public Functions
| virtual bool | event(QEvent *event) override |
32 public functions inherited from QObject
Public Slots
1 public slot inherited from QObject
Signals
2 signals inherited from QObject
Static Public Members
| QThread * | create(Function &&f, Args &&... args) |
| QThread * | create(Function &&f) |
| QThread * | currentThread() |
| Qt::HANDLE | currentThreadId() |
| int | idealThreadCount() |
| void | msleep(unsigned long msecs) |
| void | sleep(unsigned long secs) |
| void | usleep(unsigned long usecs) |
| void | yieldCurrentThread() |
11 static public members inherited from QObject
Protected Functions
9 protected functions inherited from QObject
Static Protected Members
| void | setTerminationEnabled(bool enabled = true) |
Additional Inherited Members
- 1 property inherited from QObject
Detailed Description
QThread类提供了一种独立于平台的方式来管理线程。
QThread对象管理程序中的一个控制线程。QThreads开始在run()中执行。默认情况下,run()通过调用exec()启动事件循环,并在线程内运行Qt事件循环。
通过使用QObject::moveToThread()将辅助对象移动到线程,可以使用辅助对象。
class Worker : public QObject { Q_OBJECT public slots: void doWork(const QString ¶meter) { QString result; /* ... here is the expensive or blocking operation ... */ emit resultReady(result); } signals: void resultReady(const QString &result); }; class Controller : public QObject { Q_OBJECT QThread workerThread; public: Controller() { Worker *worker = new Worker; worker->moveToThread(&workerThread); connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater); connect(this, &Controller::operate, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResults); workerThread.start(); } ~Controller() { workerThread.quit(); workerThread.wait(); } public slots: void handleResults(const QString &); signals: void operate(const QString &); };
QThread停止线程的方法:
workerThread.quit();
workerThread.wait();
###################################
QQ 3087438119

浙公网安备 33010602011771号