Qt多线程及定时器

    Qt开发中,经常使用到多线程,在官方文档中有两种方式实现多线程。

一、继承QThread

  class WorkerThread : public QThread
  {
      Q_OBJECT
      void run() 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();
  }

如上,继承QThread类,重写run(),注意,只有run中的才在次线程中运行,WorkerThread的构造函数(如果有)实在主线程中运行的,写的时候注意,否者会出现不在一个线程的错误。

二、实现一个类继承自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 &);
  };

connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);当线程结束时,销毁worker对象。

三、在线程中运行定时器

有时候需要使用定时器完成一些任务,但是在主线程中使用定时器会导致程序假死的现象

        m_timerPtzInquire = new QTimer;
        connect(m_timerPtzInquire, SIGNAL(timeout()), this, SLOT(ptzInquire()), Qt::DirectConnection);
        m_timerPtzInquire->start(200);
        m_timerPtzInquire->moveToThread(&m_inquirePtzThread);
        connect(&m_inquirePtzThread, &QThread::finished, m_timerPtzInquire, &QObject::deleteLater);
        m_inquirePtzThread.start();
使用connect的Qt::DirectConnection参数,使槽在信号发起者所在线程运行,然后运行定时器,再将其移动一个次线程中,然后使定时器线程结束时,销毁定时器对象。

你可能感兴趣的:(Qt)