QT多线程和定时器

一、定时器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:当控制权返还给接收者信号的事件循环中时,开始调用槽函数。槽函数在接收者的线程中执行。

回到我们的例子,首先将定时器所处的线程改为新建的线程,然后连接信号槽,槽函数在定时器所处的线程中执行。
本文转载自:http://www.cnblogs.com/hellovenus/p/qt_thread_timer.html

猜你喜欢

转载自blog.csdn.net/snow_rain_1314/article/details/80982913