其他分享
首页 > 其他分享> > QThread

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

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();

 

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

标签:const,void,QObject,workerThread,public,QThread
来源: https://www.cnblogs.com/herd/p/15484255.html