QT线程与定时器

本文详细介绍了Qt中QTimer类的使用方法,包括如何在多线程环境中正确使用定时器,避免线程冲突,确保定时任务在目标线程中执行。

一、定时器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()方法。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

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

<think>我们讨论的是在Qt中如何在线程中启动定时器。根据之前的示例,我们通常使用QTimer和QThread结合。但注意,在子线程中直接创建QTimer可能会遇到问题,因为QTimer需要事件循环。 正确的方法是在子线程中启动事件循环,然后在该线程中创建QTimer。 有两种常见方法: 方法1:在主线程中创建QTimer,但将timeout信号连接到子线程的槽函数(通过queued connection),并确保槽函数在子线程中执行(通过moveToThread)。 方法2:在子线程中创建QTimer,这样定时器就在子线程的事件循环中运行。 这里我们重点展示方法2:在子线程中创建并启动定时器。 步骤: 1. 创建一个工作类(Worker),它包含一个槽函数用于启动定时器(例如startTimerInThread),以及另一个槽函数用于处理定时任务(doWork)。 2. 在工作类中,我们可以在startTimerInThread槽函数中创建QTimer并连接timeout信号到doWork槽函数,然后启动定时器。注意:这个槽函数必须在子线程中调用,以确保定时器属于子线程。 3. 将工作对象移动到子线程。 4. 从主线程发送一个信号(或调用工作对象的槽函数)来触发工作对象启动定时器(即调用startTimerInThread)。 示例代码: ```cpp #include <QCoreApplication> #include <QTimer> #include <QThread> #include <QDebug> class Worker : public QObject { Q_OBJECT public: Worker() {} public slots: void startTimerInThread() { // 确保在子线程中执行 qDebug() << "Starting timer in thread:" << QThread::currentThreadId(); // 创建定时器 QTimer* timer = new QTimer(this); // 注意:父对象是this,即Worker对象,它已移动到子线程,所以定时器也在子线程 connect(timer, &QTimer::timeout, this, &Worker::doWork); timer->start(1000); // 1秒触发一次 } void doWork() { qDebug() << "Working in thread:" << QThread::currentThreadId(); // 执行定时任务 } }; int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QThread workerThread; Worker worker; worker.moveToThread(&workerThread); // 连接线程启动信号,以启动定时器 QObject::connect(&workerThread, &QThread::started, &worker, &Worker::startTimerInThread); workerThread.start(); // 退出处理 QObject::connect(&app, &QCoreApplication::aboutToQuit, &workerThread, &QThread::quit); QObject::connect(&workerThread, &QThread::finished, &workerThread, &QThread::deleteLater); return app.exec(); } #include "moc_worker.cpp" // 如果使用qmake,需要包含moc文件(或者使用自动生成) ``` 关键点: - 在Worker的startTimerInThread槽函数中创建QTimer,这样定时器就属于当前线程(子线程)。 - 通过连接子线程的started信号来触发startTimerInThread,确保当子线程的事件循环启动后再创建定时器。 - 定时器的timeout信号触发doWork槽函数,该函数在子线程中执行。 注意事项: 1. 不要在子线程中直接使用QTimer的singleShot或start,除非你确保在子线程中创建并启动定时器(即定时器属于该线程)。 2. 如果子线程没有运行事件循环(即没有调用exec()),则定时器不会触发。QThread的run()默认调用exec(),所以使用start()启动线程后,事件循环会运行。 §§相关问题§§ 1. 如果Qt线程没有事件循环,如何安全地使用定时器? 2. 在Qt中,如何确保定时器在子线程中创建并启动? 3. 在Worker类中,为什么将定时器的父对象设置为this(即Worker对象)? 4. 如何停止子线程中的定时器并安全释放资源? 5. Qt中,跨线程的信号槽连接默认是哪种类型?在定时器场景中如何保证线程安全?</think>在Qt中,线程启动定时器主要有两种方法:**使用QTimer的`start()`方法**或**使用QObject的`startTimer()`方法**。以下是具体实现和区别: ### 方法1:使用QTimer(推荐) ```cpp #include <QCoreApplication> #include <QTimer> #include <QThread> #include <QDebug> class TimerWorker : public QObject { Q_OBJECT public slots: void startTimer() { QTimer *timer = new QTimer(this); // 父对象为当前Worker connect(timer, &QTimer::timeout, this, []() { qDebug() << "定时任务执行于线程:" << QThread::currentThreadId(); }); timer->start(1000); // 启动1秒间隔的定时器 } }; int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QThread workerThread; TimerWorker worker; worker.moveToThread(&workerThread); QObject::connect(&workerThread, &QThread::started, &worker, &TimerWorker::startTimer); workerThread.start(); return app.exec(); } ``` **特点**: - 通过`QTimer::timeout`信号触发任务 - 定时器对象需在子线程中创建(`new QTimer(this)`) - 自动依赖线程事件循环 ### 方法2:使用startTimer()重载 ```cpp class TimerWorker : public QObject { Q_OBJECT protected: void timerEvent(QTimerEvent *event) override { if (event->timerId() == m_timerId) { qDebug() << "定时任务执行于线程:" << QThread::currentThreadId(); } } public: void startTimer() { m_timerId = QObject::startTimer(1000); // 返回定时器ID } private: int m_timerId = -1; }; ``` **特点**: - 需重写`timerEvent()`事件处理器 - 通过`startTimer()`返回ID管理定时器 - 需手动调用`killTimer()`停止 ### 关键区别 | **特性** | QTimer方式 | startTimer()方式 | |----------------|--------------------------------|-------------------------------| | 触发机制 | 信号槽(`timeout`信号) | 事件重写(`timerEvent`) | | 创建位置 | 必须在目标线程内创建 | 可在任意线程调用 | | 线程安全性 | 高(自动队列连接) | 需手动确保线程安全 | | 停止方式 | `timer->stop()` | `killTimer(id)` | | 适用场景 | 常规定时任务 | 需要精细控制定时器的场景 | ### 注意事项 1. **事件循环必需**:两种方法都要求线程运行事件循环(`QThread::exec()`) 2. **资源释放**:QTimer对象应设置父对象(如`this`)实现自动释放 3. **跨线程操作**:避免在定时任务中直接操作GUI,需通过信号槽通信 4. **精度限制**:定时器最小精度约5-15ms(取决于操作系统)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值