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参数,使槽在信号发起者所在线程运行,然后运行定时器,再将其移动一个次线程中,然后使定时器线程结束时,销毁定时器对象。
  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值