Qt 4.8下实现数月后触发的定时器问题求助
解决Qt 4.8中QTimer无法设置数月后触发的问题
问题说明
Qt 4.8的QTimer::start(int msec)仅接受int类型的毫秒数参数,最大值为2147483647毫秒(约24天),无法直接设置数月后的定时任务。原crontab方案已弃用,现有代码计算未来3个月的时间差后传入QTimer,因时间差超出int范围导致溢出,触发QObject::startTimer: Timers cannot have negative intervals错误。
原代码示例
uint sec_prediction, sec_now, answer; QDateTime now = QDateTime::currentDateTime(); sec_now = now.toTime_t(); QLocale mylocale(QLocale::English); qDebug() << mylocale.toString(now, "MMM d hh:mm:ss") << sec_now; QDateTime payment = QDateTime::currentDateTime(); payment = payment.addMonths(3); sec_prediction = payment.toTime_t(); qDebug() << mylocale.toString(payment, "MMM d hh:mm:ss") << sec_prediction; answer = sec_prediction - sec_now; qDebug() << answer << " - After this time, the timer should start. \n"; QTimer timer; timer.setSingleShot(true); connect(&timer, SIGNAL(timeout()), this, SLOT(processQueue())); timer.start(answer * 1000);
控制台输出
"Jan 7 00:42:38" 1673041358 "Apr 7 00:42:38" 1680817358 7776000 - After this time, the timer should start. QObject::startTimer: Timers cannot have negative intervals
解决方案
方案1:拆分长定时为多个短定时
用循环定时器定期检查当前时间与目标时间的差值,当差值小于QTimer的最大限制时,再设置单次定时器触发任务,既避免溢出又保证触发准确性。
class LongTimer : public QObject { Q_OBJECT public: LongTimer(const QDateTime& targetTime, QObject* parent = nullptr) : QObject(parent), m_targetTime(targetTime) { // 设置每天检查一次,可按需调整间隔 m_checkTimer.setInterval(86400000); connect(&m_checkTimer, SIGNAL(timeout()), this, SLOT(checkTime())); m_checkTimer.start(); // 启动时先执行一次检查 checkTime(); } private slots: void checkTime() { qint64 remaining = QDateTime::currentDateTime().msecsTo(m_targetTime); if (remaining <= 0) { m_checkTimer.stop(); emit timeout(); return; } // 剩余时间在QTimer允许范围内,直接设置单次定时器 if (remaining <= INT_MAX) { m_checkTimer.stop(); QTimer::singleShot(remaining, this, SIGNAL(timeout())); } // 否则继续按原周期检查 } signals: void timeout(); private: QDateTime m_targetTime; QTimer m_checkTimer; }; // 使用示例 QDateTime payment = QDateTime::currentDateTime().addMonths(3); LongTimer* longTimer = new LongTimer(payment, this); connect(longTimer, SIGNAL(timeout()), this, SLOT(processQueue()));
方案2:定期检查系统时间(适合精度要求不高的场景)
用短周期定时器(比如每分钟)定期检查当前时间是否到达目标时间,到达则触发任务并停止检查,实现简单但可能存在几分钟误差。
QTimer* checkTimer = new QTimer(this); QDateTime targetTime = QDateTime::currentDateTime().addMonths(3); connect(checkTimer, SIGNAL(timeout()), this, [=]() { if (QDateTime::currentDateTime() >= targetTime) { checkTimer->stop(); processQueue(); } }); // 每分钟检查一次 checkTimer->start(60000);
方案3:自定义支持64位时长的定时器
基于QObject::timerEvent实现支持超长时长的定时器,把总时长拆分成多个int范围的时段,逐段触发直到时长耗尽。
class BigTimer : public QObject { Q_OBJECT public: BigTimer(QObject* parent = nullptr) : QObject(parent), m_remaining(0), m_timerId(-1) {} void start(qint64 msec) { if (m_timerId != -1) killTimer(m_timerId); if (msec <= 0) { emit timeout(); return; } if (msec <= INT_MAX) { m_timerId = startTimer(static_cast<int>(msec)); } else { m_remaining = msec; // 先启动一个最大时长的定时器 m_timerId = startTimer(INT_MAX); } } protected: void timerEvent(QTimerEvent* event) override { if (event->timerId() != m_timerId) return; killTimer(m_timerId); m_timerId = -1; if (m_remaining > 0) { m_remaining -= INT_MAX; start(m_remaining); } else { emit timeout(); } } signals: void timeout(); private: qint64 m_remaining; int m_timerId; }; // 使用示例 BigTimer* bigTimer = new BigTimer(this); QDateTime now = QDateTime::currentDateTime(); QDateTime payment = now.addMonths(3); qint64 msecs = now.msecsTo(payment); bigTimer->start(msecs); connect(bigTimer, SIGNAL(timeout()), this, SLOT(processQueue()));
内容的提问来源于stack exchange,提问作者Roman Khudoberdin
相关产品推荐
相关产品推荐

