Proper way to code a stopwatch
-
I'm trying to make a stopwatch for an app I'm currently working at. I've managed to do it by using a QTimer and QTime. The step/refresh rate is 10ms. I did it by connecting a QTimer's timeout signal to a lambda slot that updates the QTime object.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), time(new QTime(0,0)), timer(new QTimer()) { ui->setupUi(this); timer->start(10); connect(timer,&QTimer::timeout,[this](){ *time=time->addMSecs(10); ui->label->setText(time->toString("mm:ss:zzz")); }); }
The code works well apparently but I'm wondering if there's a smarter and maybe more efficient way of doing it.
-
That doesn't look correct at all. By default QTimer is
Qt::CoarseTimer
, which means it won't emit the signal at exactly 10ms, just somewhere near. A second problem is that signal delivery can stall sometimes. So adding 10ms in the slot is not a good way to do it as it might be actually 11 or more that passed and after some seconds the error will sum up to noticeable sizes.The correct way to do it is to measure time at the start and then at the intervals check how much time has passed from that starting point. This way you won't accumulate any error.
There's a QElapsedTimer class exactly for that purpose. You start the timer and then, in your slot, call elapsed() to get the number of milliseconds that passed since the start. -
Hi,
Are-you thinking about something like fromMSecsSinceStartOfDay ?
-
Yes SGaist, that's what I was looking for.
I wrote the timer using QElapsedTimer but have now to figure out how to implement a pause->resume command. I'm thinking of putting the so far elapsed time into a variable(toPause) when clicking pause, and when clicking resume to restart the elapsedTimer. In the label I'd print toPause+elapsedTimer.elapsed(). I think it'll work but I'm not sure how accurate it'll be. -
Yes SGaist, that's what I was looking for.
I wrote the timer using QElapsedTimer but have now to figure out how to implement a pause->resume command. I'm thinking of putting the so far elapsed time into a variable(toPause) when clicking pause, and when clicking resume to restart the elapsedTimer. In the label I'd print toPause+elapsedTimer.elapsed(). I think it'll work but I'm not sure how accurate it'll be.@cpper said in Proper way to code a stopwatch:
I think it'll work but I'm not sure how accurate it'll be
That should be fine. "Clicking" is not a very accurate operation in itself and users of stopwatches don't usually press pause/resume thousands of times in one measure. I'd say it's gonna be as accurate as the user can be.