2010-04-23 241 views
5

在我的一个项目中,我正在使用QTimer,我想知道是否有可能获得QTimer的剩余时间,以便让用户知道“时间直到下次超时:10秒”或像那样的东西...这可能吗?如果不是这样,有没有人有如何实现这一点的好点子?QTimer的剩余时间

也许我得写我自己的定时器......

+0

有[REMAININGTIME](http://qt-project.org/doc/qt-5.0/qtcore/qtimer.html#remainingTime-prop )在Qt5中的属性 – 2013-02-26 12:01:45

回答

6

这是你在找什么? QTimer :: elapsed()使用计算机时钟,因此根据您的平台,准确度会有所不同。

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

看一看从QObject中的timerEvent事件。我认为你可以用这个达到你想要的。

3

感谢您的建议,但我找到了另一种解决方案。我写了我自己的类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); 
} 
+1

不是一个坏的方法,但如果你打算这样做,我会将更多的它封装到my_timer类中。例如,有一个every_second信号和final_timeout信号,以便使用它的类不必获取通知程序定时器并连接到它。您还可以跟踪该课程的时间和剩余时间。 – 2010-04-23 16:08:33

1

冷杉完整性起见:

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