c/c++语言开发共享Qt实现线程与定时器的方法

目录一、定时器qtimer类二、在多线程中使用qtimer1.错误用法2.正确用法一3.正确用法二一、定时器qtimer类the qtimer class provides repetitive an

目录
  • 一、定时器qtimer类
  • 二、在多线程中使用qtimer
    • 1.错误用法
    • 2.正确用法一
    • 3.正确用法二

一、定时器qtimer类

the qtimer class provides repetitive and single-shot timers.

the qtimer class provides a high-level programming interface for timers. to use it, create a qtimer, connect its timeout() signal to the appropriate slots, and call start(). from then on, it will emit the timeout() signal at constant intervals.

上面这段话摘自qt助手文档,我们使用qtimer类定义一个定时器,它可以不停重复,也可以只进行一次便停止。

使用起来也很简单:

qtimer *timer = new qtimer(this);  connect(timer, signal(timeout()), this, slot(update()));  timer->start(1000);

创建一个qtimer对象,将信号timeout()与相应的槽函数相连,然后调用start()函数。接下来,每隔一段时间,定时器便会发出一次timeout()信号。

更多用法这里就不讲了,您可以自行参考官方文档。比如如何停止、如何令定时器只运行一次等。

二、在多线程中使用qtimer

1.错误用法

您可能会这么做:

子类化qthread,在线程类中定义一个定时器,然后在run()方法中调用定时器的start()方法。

testthread::testthread(qobject *parent)      : qthread(parent)  {      m_ptimer = new qtimer(this);      connect(m_ptimer, &qtimer::timeout, this, &testthread::timeoutslot);  }     void testthread::run()  {      m_ptimer->start(1000);  }     void testthread::timeoutslot()  {      qdebug() << qstring::fromlocal8bit("当前线程id:") << qthread::currentthread();  }

接下来在主线程中创建该线程对象,并调用它的start()方法:

m_pthread = new testthread(this);  m_pthread->start();

看似十分自然,没有什么不妥,然而,编译器将通知下面的错误信息:

 qobject::starttimer: timers cannot be started from another thread 

——定时器不能被其它线程start。

我们来分析一下:

刚开始只有主线程一个,testthread的实例是在主线程中创建的,定时器在testthread的构造函数中,所以也是在主线程中创建的。

当调用testthread的start()方法时,这时有两个线程。定时器的start()方法是在另一个线程中,也就是testthread中调用的。

创建和调用并不是在同一线程中,所以出现了错误。

具体的原理可参考官方文档——点我

每个qobject实例都有一个叫做“线程关系”(thread affinity)的属性,或者说,它处于某个线程中。

默认情况下,qobject处于创建它的线程中。

当qobject接收队列信号(queued signal)或者传来的事件(posted event),槽函数或事件处理器将在对象所处的线程中执行。

根据以上的原理,qt使用计时器的线程关系(thread affinity)来决定由哪个线程发出timeout()信号。正因如此,你必须在它所处的线程中start或stop该定时器,在其它线程中启动定时器是不可能的。

2.正确用法一

在testthread线程启动后创建定时器。

void testthread::run()  {      m_ptimer = new qtimer();      m_ptimer->setinterval(1000);      connect(m_ptimer, &qtimer::timeout, this, &testthread::timeoutslot);      m_ptimer->start();      this->exec();  }

有些地方需要注意:

1.不能像下面这样给定时器指定父对象

m_ptimer = new qtimer(this);

否则会出现以下警告:

qobject: cannot create children for a parent that is in a different thread.
(parent is testthread(0x709d88), parent's thread is qthread(0x6e8be8), current thread is testthread(0x709d88) 

因为testthread对象是在主线程中创建的,它的qobject子对象也必须在主线程中创建。所以不能指定父对象为testthread。

2.必须要加上事件循环exec()

否则线程会立即结束,并发出finished()信号。

另外还有一点需要注意,与start一样,定时器的stop也必须在testthread线程中,否则会出错。

  void testthread::timeoutslot()  {      m_ptimer->stop();      qdebug() << qstring::fromlocal8bit("当前线程id:") << qthread::currentthread();  }

上面的代码将出现以下错误:

qobject::killtimer: timers cannot be stopped from another thread

综上,子类化线程类的方法可行,但是不太好。 

3.正确用法二

无需子类化线程类,通过信号启动定时器。

testclass::testclass(qwidget *parent)      : qwidget(parent)  {      m_pthread = new qthread(this);      m_ptimer = new qtimer();      m_ptimer->movetothread(m_pthread);      m_ptimer->setinterval(1000);      connect(m_pthread, signal(started()), m_ptimer, slot(start()));      connect(m_ptimer, &qtimer::timeout, this, &threadtest::timeoutslot, qt::directconnection);  }

通过movetothread()方法改变定时器所处的线程,不要给定时器设置父类,否则该函数将不会生效。

在信号槽连接时,我们增加了一个参数——连接类型,先看看该参数可以有哪些值:

  • qt::autoconnection:默认值。如果接收者处于发出信号的线程中,则使用qt::directconnection,否则使用qt::queuedconnection,连接类型由发出的信号决定。
  • qt::directconnection:信号发出后立即调用槽函数,槽函数在发出信号的线程中执行。
  • qt::queuedconnection:当控制权返还给接收者信号的事件循环中时,开始调用槽函数。槽函数在接收者的线程中执行。

回到我们的例子,首先将定时器所处的线程改为新建的线程,然后连接信号槽,槽函数在定时器所处的线程中执行。

到此这篇关于qt实现线程与定时器的方法的文章就介绍到这了,更多相关qt 线程与定时器 内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享Qt实现线程与定时器的方法,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1038056.html

(0)
上一篇 2022年1月25日
下一篇 2022年1月25日

精彩推荐