我需要Qtimer的使用方面的一些帮助。如何暂停和恢复Qtimer(Qt 5)
我使用Qt 5.0.2,在这里我的问题的工作:
我试图建立一个定时器,界面很简单:
只是有2按钮:在“开始”按钮,启动计时器,“暂停”按钮,以及QtimeEdit来显示时间。
此屏幕快照显示它的样子:http://img834.imageshack.us/img834/1046/5ks6.png
的问题是,暂停功能不能正常工作。我已阅读所有有关Qtimer的文档:http://harmattan-dev.nokia.com/docs/library/html/qt4/qtimer.html和here:qt.developpez.com/doc/5.0-snapshot/qtimer/,但没有结果。
这是源代码,我有:(我只放所需要的)
// 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();
}
}
当我按下启动按钮,计时器开始很好,但是当我按下“暂停”,它只是暂停在图形界面上,但是当我恢复时,它显示当前时间,好像它没有暂停。
例如:
我开始。 我在00:05暂停。它显然阻塞了定时器。 我等了10秒钟。我恢复计时器,它显示00:15而不是00:06
我该如何解决这个问题?
谢谢!
编辑:谢谢库巴奥伯,但你能解释我的代码吗?
暂停是如何工作的?
不,你没有把只在所需的内容。你只会把你认为**所需要的东西放进去。 – Greenflow
为什么每次开始模拟时都会创建一个新的'QTimer'对象? 'timeElapsed'在哪里得到它的价值? – thuga
在你的代码中有很多问题,但最重要的问题是你没有显示的'updateTime()'插槽。 –