Skip to content

Commit 2cbb5c9

Browse files
authored
Fix timer using 32-bit ints instead of 64-bit ints (#487)
1 parent 08fd15a commit 2cbb5c9

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

include/aoclocklabel.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <QLabel>
55
#include <QBasicTimer>
66
#include <QTimerEvent>
7-
#include <QTime>
7+
#include <QDateTime>
88
#include <QDebug>
99

1010
class AOClockLabel : public QLabel {
@@ -13,8 +13,8 @@ class AOClockLabel : public QLabel {
1313
public:
1414
AOClockLabel(QWidget *parent);
1515
void start();
16-
void start(int msecs);
17-
void set(int msecs, bool update_text = false);
16+
void start(qint64 msecs);
17+
void set(qint64 msecs, bool update_text = false);
1818
void pause();
1919
void stop();
2020

@@ -23,7 +23,7 @@ class AOClockLabel : public QLabel {
2323

2424
private:
2525
QBasicTimer timer;
26-
QTime target_time;
26+
QDateTime target_time;
2727
};
2828

2929
#endif // AOCLOCKLABEL_H

src/aoclocklabel.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ void AOClockLabel::start()
77
timer.start(1000 / 60, this);
88
}
99

10-
void AOClockLabel::start(int msecs)
10+
void AOClockLabel::start(qint64 msecs)
1111
{
1212
this->set(msecs);
1313
this->start();
1414
}
1515

16-
void AOClockLabel::set(int msecs, bool update_text)
16+
void AOClockLabel::set(qint64 msecs, bool update_text)
1717
{
18-
target_time = QTime::currentTime().addMSecs(msecs);
18+
target_time = QDateTime::currentDateTime().addMSecs(msecs);
1919
if (update_text)
2020
{
21-
if (QTime::currentTime() >= target_time)
21+
if (QDateTime::currentDateTime() >= target_time)
2222
{
2323
this->setText("00:00:00.000");
2424
}
2525
else
2626
{
27-
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
27+
qint64 ms_left = QDateTime::currentDateTime().msecsTo(target_time);
28+
QTime timeleft = QTime(0, 0).addMSecs(ms_left % (1000 * 3600 * 24));
2829
QString timestring = timeleft.toString("hh:mm:ss.zzz");
2930
this->setText(timestring);
3031
}
@@ -45,12 +46,13 @@ void AOClockLabel::stop()
4546
void AOClockLabel::timerEvent(QTimerEvent *event)
4647
{
4748
if (event->timerId() == timer.timerId()) {
48-
if (QTime::currentTime() >= target_time)
49+
if (QDateTime::currentDateTime() >= target_time)
4950
{
5051
this->stop();
5152
return;
5253
}
53-
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
54+
qint64 ms_left = QDateTime::currentDateTime().msecsTo(target_time);
55+
QTime timeleft = QTime(0, 0).addMSecs(ms_left % (1000 * 3600 * 24));
5456
QString timestring = timeleft.toString("hh:mm:ss.zzz");
5557
this->setText(timestring);
5658
} else {

0 commit comments

Comments
 (0)