2010-04-23 19 views
5

En uno de mis proyectos que estoy trabajando con un QTimer y yo Wonderer si es posible obtener el tiempo restante de un QTimer con el fin de que el usuario sepa "Tiempo hasta el próximo tiempo de espera: 10 seg" o algo así ... ¿Es eso posible? Si no es así, ¿alguien tiene buenas ideas sobre cómo darse cuenta de eso?Tiempo restante del QTimer

Tal vez tiene que escribir mi propio temporizador ...

+0

Hay [REMAININGTIME] (http://qt-project.org/doc/qt-5.0/qtcore/qtimer.html#remainingTime-prop) propiedad de QT5 –

Respuesta

6

¿Es esto lo que busca? QTimer :: elapsed() usa el reloj de la computadora, por lo que dependiendo de su plataforma, la precisión varía.

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; 
} 
2

Tenga una mirada a la timerEvent evento desde QObject. Creo que puedes lograr lo que quieras con esto.

3

Gracias por su consejo, pero he encontrado otra solución. Escribí mi propia clase my_timer, que simplemente tiene el temporizador secundario interno y se apaga cada segundo. En mi ventana principal, conecto este tiempo de espera con una función que actualiza la pantalla del usuario.

El 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(); 
} 

Y en mi 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); 
} 
+1

No es un mal enfoque, pero si usted va a hacer esto, yo encapsular más de lo mismo en la clase my_timer. Por ejemplo, tenga una señal de cada dos segundos y señal de final_timeout, para que las clases que lo usan no tengan que obtener el temporizador del notificador y conectarse a él. También podría hacer un seguimiento del tiempo transcurrido y el tiempo restante en esa clase. –

1

abeto aras de la exhaustividad:

#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_*/ 
Cuestiones relacionadas