QTimer Remaining Time

In one of my projects, I work with QTimer, and I wonder if it is possible to get the remaining QTimer time so that the user knows "Time to the next timeout: 10 seconds" or something like that ... Is this possible? If not, does anyone have any good ideas on how to implement this?

Maybe I need to write my own timer ...

+5
source share
4 answers

Is this what you are looking for? QTimer :: elapsed () uses a computer clock, so the accuracy varies depending on your platform.

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;
}
+6
source

, . my_timer, , . .

my_timer.cpp:

#include "my_timer.hpp"

my_timer::my_timer( QWidget *parent ) : QTimer( parent )
{
    notifier = new QTimer;
}

my_timer::~my_timer()
{
    //...
}

QTimer* my_timer::get_notifier()
{
    return notifier;
}

void my_timer::start( int msec )
{
    QTimer::start( msec );
    notifier->start( 1000 );
}

void my_timer::stop()
{
    QTimer::stop();
    notifier->stop();
}

main_window.cpp:

void main_window::setup_connects()
{
        // ...
    connect( m_timer->get_notifier(), SIGNAL(timeout()), this, SLOT(on_update_label()) );
        // ...
}

void main_window::on_update_label()
{
    if( m_timer->isActive() )
    {
        if( remaining_secs > 1 )
        {
            remaining_secs--;   
        }
        else
        {
            remaining_secs = spin_box->value();
        }

        update_label();
    }
}

void main_window::update_label()
{
    m_time_string = QString( "Remaining time until next execution: %1" ).arg( remaining_secs );
    m_time_label->setText( m_time_string );
}
+3

Look at the event timerEventfrom QObject. I think you can achieve what you want with this.

+2
source

for completeness:

#ifndef _ELAPSED_TIMER_H_
#define _ELAPSED_TIMER_H_

#include <QTimer>
#include <QTime>

/* 
 * convenience class, which can return the proportion of the time left. usefull for interpolation
 * tasks
 **/
class ElapsedTimer : public QTimer
{
    Q_OBJECT

    public:
        ElapsedTimer(QObject* parent) : QTimer(parent)
        {
            connect(this, SIGNAL(timeout()), this, SLOT(resettime()));
        }

        void start()
        {
            m_time.start();
            QTimer::start();
        }

        void start(int msec)
        {
            m_time.start();
            QTimer::start(msec);
        }

        double proportionLeft()
        {
            return (interval()-m_time.elapsed())/interval();
        }

        int timeLeft()
        {
            return interval()-m_time.elapsed();
        }

    private slots:

        void resettime()
        {
            m_time.restart();
        }

    private:
        QTime m_time;
};

#endif/*_ELAPSED_TIMER_H_*/
+1
source

All Articles