QThread

How to use QThread Class

The QThread class provides a platform-independent way to manage threads. More…
QTthread類提供了一種獨立於平臺的管理線程的方法。詳見:Detailed Description
Header文件中需要添加
#include < QThread >
pro(qmake)文件中需要添加
QT += core
Inherits(繼承):QObject

Public Types

enum QThread::Priority(枚舉)

This enum type indicates how the operating system should schedule newly created threads.(此枚舉類型指示操作系統應如何計劃新創建的線程。)

Constant(常量) Value(值) Description(描述)
QThread::IdlePriority 0 scheduled only when no other threads are running.(僅在沒有其他線程正在運行時調度)
QThread::LowestPriority 1 scheduled less often than LowPriority.(計劃的次數少於LowPriority)
QThread::LowPriority 2 scheduled less often than NormalPriority.(計劃的次數少於NormalPriority)
QThread::NormalPriority 3 the default priority of the operating system.(操作系統默認優先級)
QThread::HighPriority 4 scheduled more often than NormalPriority.(計劃的次數高於NormalPriority)
QThread::HighestPriority 5 scheduled more often than HighPriority.(計劃的次數高於HighPriority)
QThread::TimeCriticalPriority 6 scheduled as often as possible.(儘可能經常安排)
QThread::InheritPriority 7 use the same priority as the creating thread. This is the default.(使用與創建線程相同的優先級。 這是默認值。)

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

The QThread class provides a platform-independent way to manage threads.
QThread類提供了一種獨立於平臺的管理線程的方法。
A QThread object manages one thread of control within the program.
QThread對象管理程序中的一個控制線程。
QThreads begin executing in run().
QThreads開始在run()中執行。
By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread.
默認情況下,run()通過調用exec()啓動事件循環並在線程內運行Qt事件循環。
You can use worker objects by moving them to the thread using QObject::moveToThread().
你可以使用QObject :: moveToThread()將worker對象移動到線程來使用它們。

  class Worker : public QObject  //定義一個Worker,集成子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      //定義一個Controller類
  {
      Q_OBJECT
      QThread workerThread;               //聲明一個workThread對象
  public:
      Controller()
       {
          Worker *worker = new Worker;   //聲明一個worker指針
          worker->moveToThread(&workerThread);//把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 &);
  };

The code inside the Worker’s slot would then execute in a separate thread.
連接到任何線程中任何對象的任何信號。
However, you are free to connect the Worker’s slots to any signal, from any object, in any thread.
但是,您可以自由地將Worker的插槽連接到任何線程中任何對象的任何信號。
It is safe to connect signals and slots across different threads, thanks to a mechanism called queued connections.
由於稱爲排隊連接的機制,可以安全地跨不同線程連接信號和插槽。
Another way to make code run in a separate thread, is to subclass QThread and reimplement run().
使代碼在單獨的線程中運行的另一種方法是繼承QThread並重新實現run()。
For example:
例如:

class WorkerThread : public QThread
{
	Q_OBJECT
	void run() Q_DECL_OVERRIDE
	{
        	QString result;
          	/* ... here is the expensive or blocking operation ... */
          	emit resultReady(result);
	}
signals:
      void resultReady(const QString &s);
};
void MyObject::startWorkInAThread()
{
	WorkerThread *workerThread = new WorkerThread(this);
	connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults);
	connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
	workerThread->start();
}

Managing Threads

List of all members, including inherited members

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章