In one of my projects I am working with a QTimer and I wonderer whether it is possible to get the remaining time of a QTimer in order to let the user know "Time until next timeout: 10 secs" or something like that... Is that possible? If not so, has anyone good ideas for how to realize that?
Maybe I got to write my own Timer...
Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations. The accuracy also depends on the timer type. For Qt::PreciseTimer, QTimer will try to keep the accurance at 1 millisecond.
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.
[slot] void QTimer::stop() Stops the timer. See also start().
The QTimer::singleShot is used to call a slot/lambda asynchronously after n ms. The basic syntax is : QTimer::singleShot(myTime, myObject, SLOT(myMethodInMyObject())); with myTime the time in ms, myObject the object which contain the method and myMethodInMyObject the slot to call.
Is this what you are looking for ? QTimer::elapsed() uses the computers clock so depending on your platform the accuracy varies.
class MyTimer : QTimer
{
MyTimer(QObject* parent) : QTimer(parent)
{
connect(this, timeout(), this, resettime());
}
int start()
{
m_time.start();
return QTimer::start();
}
int start(int msec)
{
m_time.start();
return QTimer::start(msec)l
}
int timeLeft()
{
return interval()-m_time.elapsed()
}
private slots:
void resettime()
{
m_time.restart();
}
private:
QTime m_time;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With