如何暂停和恢复 Qtimer (Qt 5) [英] How to pause and resume a Qtimer (Qt 5)

查看:145
本文介绍了如何暂停和恢复 Qtimer (Qt 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于 Qtimer 使用的帮助.

I need some help on the usage of Qtimer.

我使用 Qt 5.0.2,这里是我的问题:

I work with Qt 5.0.2 and here my problem :

我正在尝试开发一个定时器,界面很简单:

I am trying to develop a Timer, and the interface is simple :

只有 2 个按钮:用于启动计时器的开始"按钮、暂停"按钮和用于显示时间的 QtimeEdit.

There is just 2 button : the button "Start", to launch the timer, and the "Pause" Button, and a QtimeEdit to display the time.

此屏幕截图显示了它的样子:http://img834.imageshack.us/img834/1046/5ks6.png

This screenshot shows how it looks like : http://img834.imageshack.us/img834/1046/5ks6.png

问题是暂停功能不起作用.我在这里阅读了关于 Qtimer 的所有文档:http://harmattan-dev.nokia.com/docs/library/html/qt4/qtimer.html 和这里: qt.developpez.com/doc/5.0-snapshot/qtimer/,但没有结果.

The problem is that the pause function doesn't work. I have read all the documentation about Qtimer here : http://harmattan-dev.nokia.com/docs/library/html/qt4/qtimer.html and here : qt.developpez.com/doc/5.0-snapshot/qtimer/ , but no result.

这是我的源代码:(我只放了需要的)

This is the source code I have : (I put only what is needed)

// Creation of the Buttons and the time area
void MainWindow::createBottom()
{

    bottom = new QWidget();

    play = new QPushButton("Launch",this);
    pause = new QPushButton("Pause",this);
    play->setDisabled(false);
    pause->setDisabled(true);
    timeEdit = new QTimeEdit(this);
    timeEdit->setDisplayFormat("mm:ss");

    layout->addWidget(play);
    layout->addWidget(pause);
    layout->addWidget(timeEdit );
    bottom->setLayout(layout);

    connect(play, SIGNAL(clicked()), this, SLOT(startSimulation()));
    connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
}

// to resume the timer where is was stopped
void MainWindow::resumeSimulation()
{
    timer->blockSignals( false );
    pause->setText("Pause");
    pause->disconnect(SIGNAL(clicked()));
    connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
    paused = false;

    timer->start();
    int timeOfPause = time->restart();
    int timeTotal = timeOfPause + timeElapsed;
    time->addMSecs(-timeTotal);

}

// to Start the timer
void MainWindow::pauseSimulation()
{
    timer->blockSignals(true);
    pause->setText("Resume");
    timer->stop();
    play->setDisabled(false);
    //pause->setDisabled(true);
    pause->disconnect(SIGNAL(clicked()));

    connect(pause, SIGNAL(clicked()), this, SLOT(resumeSimulation())); 
    paused = true;
}

// to Start the timer from zero.
void MainWindow::startSimulation()
{

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this , SLOT(updateTime()));
    timer->start(500);
    play->setDisabled(true);

    pause->setDisabled(false);
}

void MainWindow::updateTime()
{
    if(time == NULL)
    {
       time = new QTime(0,0,0,0);
       time->start();
    }
    //timeEdit->setTime(QTime::fromS(time->elapsed()));
    //time = &(time->addMSecs(1000));
    if(hasRestart)
    {
        time->restart();
        time->addMSecs(-timeElapsed);
        hasRestart = false;
    }
    else
    {
        timeElapsed =+ time->elapsed();
    }
    int seconds = 0;
    int minutes = 0;
    int hours = 0;

    if(!paused)
    {
            seconds = (timeElapsed/1000)%60;
            minutes = (timeElapsed/60000)%60;
            hours =  (timeElapsed/3600000)%24;
            std::cout << "Test : " << hours << ":" << minutes << ":" << seconds << std::endl;
            timeEdit->setTime(QTime(0,minutes,seconds,0));
            timeEdit->update();
    }
}

当我按下开始按钮时,计时器开始正常,但是当我按下暂停"时,它只会在图形界面上暂停它,但是当我恢复时,它显示当前时间,好像它没有暂停.

When I push the Start button, the timer starts well, but when I push "Pause" it only pause it on the graphic interface, but when I resume, it shows the present time as if it hadn't paused.

例如:

我开始.我在 00:05 暂停.它显然阻止了计时器.我等了10秒钟.我恢复计时器,它显示 00:15 而不是 00:06

I start. I pause at 00:05. It blocks apparently the timer. I wait for 10 seconds. I resume the timer, it shows 00:15 instead of 00:06

我该如何解决?

谢谢!

谢谢 Kuba Ober,但你能解释一下你发布的代码吗?

暂停是如何工作的?

推荐答案

下面是一个 SSCCE,在 Qt 4.8 下测试和 5.1.

Below is a SSCCE, tested under both Qt 4.8 and 5.1.

//main.cpp
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QElapsedTimer>
#include <QTime>

class Window : public QWidget {
    Q_OBJECT
    int m_timerId;
    qint64 m_accumulator;
    QLabel *m_label;
    QElapsedTimer m_timer;
    Q_SLOT void on_restart_clicked() {
        m_accumulator = 0;
        m_timer.restart();
        if (m_timerId == -1) m_timerId = startTimer(50);
    }
    Q_SLOT void on_pause_clicked() {
        if (m_timer.isValid()) {
            m_accumulator += m_timer.elapsed();
            m_timer.invalidate();
        } else {
            m_timer.restart();
            m_timerId = startTimer(50);
        }
    }
    void timerEvent(QTimerEvent * ev) {
        if (ev->timerId() != m_timerId) {
            QWidget::timerEvent(ev);
            return;
        }
        QTime t(0,0);
        t = t.addMSecs(m_accumulator);
        if (m_timer.isValid()) {
            t = t.addMSecs(m_timer.elapsed());
        } else {
            killTimer(m_timerId);
            m_timerId = -1;
        }
        m_label->setText(t.toString("h:m:ss.zzz"));
    }
public:
    explicit Window(QWidget *parent = 0, Qt::WindowFlags f = 0) : QWidget(parent, f), m_timerId(-1)  {
        QVBoxLayout * l = new QVBoxLayout(this);
        QPushButton * restart = new QPushButton("Start");
        QPushButton * pause = new QPushButton("Pause/Resume");
        restart->setObjectName("restart");
        pause->setObjectName("pause");
        m_label = new QLabel("--");
        l->addWidget(restart);
        l->addWidget(pause);
        l->addWidget(m_label);
        QMetaObject::connectSlotsByName(this);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window w;
    w.show();
    return a.exec();
}

#include "main.moc"

这篇关于如何暂停和恢复 Qtimer (Qt 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆