代码之家  ›  专栏  ›  技术社区  ›  Exa

季度剩余时间

  •  6
  • Exa  · 技术社区  · 15 年前

    在我的一个项目中,我正在使用一个qtimer,我想知道是否可以获得qtimer的剩余时间,以便让用户知道“直到下一次超时的时间:10秒”或类似的事情…有可能吗?如果不是这样,有没有人对如何认识到这一点有好的想法?

    也许我得自己写计时器…

    4 回复  |  直到 13 年前
        1
  •  6
  •   Harald Scheirich    15 年前

    这就是你要找的吗?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
  •  3
  •   Exa    15 年前

    谢谢你的建议,但我找到了另一个解决办法。我写了我自己的课,我的计时器,简单地说,它在内部的第二计时器上,每秒钟超时一次。在我的主窗口中,我将这个超时与一个为用户更新显示的函数连接起来。

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

    在我的主窗口.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
  •  2
  •   Patrice Bernassola    15 年前

    看看 timerEvent 来自QObject的事件。我认为你能用这个达到你想要的。

        4
  •  1
  •   marvin2k    13 年前

    为了完整起见:

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