Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remaining time of QTimer

Tags:

qt

qt4

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...

like image 427
Exa Avatar asked Apr 23 '10 08:04

Exa


People also ask

How accurate is QTimer?

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.

What is QTimer in Qt?

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.

How do I stop QTimer?

[slot] void QTimer::stop() Stops the timer. See also start().

What is QTimer singleShot?

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.


1 Answers

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;
}
like image 156
Harald Scheirich Avatar answered Sep 21 '22 10:09

Harald Scheirich