Fix timer using 32-bit ints instead of 64-bit ints (#487)

This commit is contained in:
oldmud0 2021-03-06 15:01:54 -06:00 committed by GitHub
parent 08fd15acda
commit 2cbb5c95ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 11 deletions

View File

@ -4,7 +4,7 @@
#include <QLabel> #include <QLabel>
#include <QBasicTimer> #include <QBasicTimer>
#include <QTimerEvent> #include <QTimerEvent>
#include <QTime> #include <QDateTime>
#include <QDebug> #include <QDebug>
class AOClockLabel : public QLabel { class AOClockLabel : public QLabel {
@ -13,8 +13,8 @@ class AOClockLabel : public QLabel {
public: public:
AOClockLabel(QWidget *parent); AOClockLabel(QWidget *parent);
void start(); void start();
void start(int msecs); void start(qint64 msecs);
void set(int msecs, bool update_text = false); void set(qint64 msecs, bool update_text = false);
void pause(); void pause();
void stop(); void stop();
@ -23,7 +23,7 @@ protected:
private: private:
QBasicTimer timer; QBasicTimer timer;
QTime target_time; QDateTime target_time;
}; };
#endif // AOCLOCKLABEL_H #endif // AOCLOCKLABEL_H

View File

@ -7,24 +7,25 @@ void AOClockLabel::start()
timer.start(1000 / 60, this); timer.start(1000 / 60, this);
} }
void AOClockLabel::start(int msecs) void AOClockLabel::start(qint64 msecs)
{ {
this->set(msecs); this->set(msecs);
this->start(); this->start();
} }
void AOClockLabel::set(int msecs, bool update_text) void AOClockLabel::set(qint64 msecs, bool update_text)
{ {
target_time = QTime::currentTime().addMSecs(msecs); target_time = QDateTime::currentDateTime().addMSecs(msecs);
if (update_text) if (update_text)
{ {
if (QTime::currentTime() >= target_time) if (QDateTime::currentDateTime() >= target_time)
{ {
this->setText("00:00:00.000"); this->setText("00:00:00.000");
} }
else else
{ {
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time)); qint64 ms_left = QDateTime::currentDateTime().msecsTo(target_time);
QTime timeleft = QTime(0, 0).addMSecs(ms_left % (1000 * 3600 * 24));
QString timestring = timeleft.toString("hh:mm:ss.zzz"); QString timestring = timeleft.toString("hh:mm:ss.zzz");
this->setText(timestring); this->setText(timestring);
} }
@ -45,12 +46,13 @@ void AOClockLabel::stop()
void AOClockLabel::timerEvent(QTimerEvent *event) void AOClockLabel::timerEvent(QTimerEvent *event)
{ {
if (event->timerId() == timer.timerId()) { if (event->timerId() == timer.timerId()) {
if (QTime::currentTime() >= target_time) if (QDateTime::currentDateTime() >= target_time)
{ {
this->stop(); this->stop();
return; return;
} }
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time)); qint64 ms_left = QDateTime::currentDateTime().msecsTo(target_time);
QTime timeleft = QTime(0, 0).addMSecs(ms_left % (1000 * 3600 * 24));
QString timestring = timeleft.toString("hh:mm:ss.zzz"); QString timestring = timeleft.toString("hh:mm:ss.zzz");
this->setText(timestring); this->setText(timestring);
} else { } else {