天天看點

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

void ​​quit​​()
void ​​start​​(QThread::Priority priority = InheritPriority)
void ​​terminate​​()

1 public slot inherited from ​​QObject​​

Signals

void ​​finished​​()
void ​​started​​()

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

int ​​exec​​()
virtual void ​​run​​()

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 &parameter) {
          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();      

###################################

下一篇: 大白 1