How to use QTimer for pausing and resuming ?
Hello,
I need some help on the usage of Qtimer.
I work with Qt 5.0.2 and here my problem :
I am trying to develop a Timer, and the interface is simple :
There is just 2 button : the button "Start", to launch the timer, and the "Pause" Button, and a QtimeEdit to display the time.
This screenshot shows how it looks like : http://img834.imageshack.us/img834/1046/5ks6.png
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/...t4/qtimer.html and here : http://qt.developpez.com/doc/5.0-snapshot/qtimer/ , but no result.
This is the source code I have : (I put only what is needed)
Code:
// Creation of the Buttons and the time area
void MainWindow::createBottom()
{
play->setDisabled(false);
pause->setDisabled(true);
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()
{
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();
}
}
time, timer, and hasRestart are defined in a header file :
QTime* time;
QTimer *timer;
boolean hasRestart;
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.
For instance :
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
How could I fix that ?
Thank you !
EDIT : I have added some code that was missing in the updateTime() method.
Re: How to use QTimer for pausing and resuming ?
Don't block signals, just call stop() on the timer. And don't disconnect any signals. Make the connections once in the constructor and don't modify them afterwards.
Re: How to use QTimer for pausing and resuming ?
Thanks wysota I have commented the lines :
Code:
void MainWindow::resumeSimulation()
{
//timer->blockSignals( false );
...
}
void MainWindow::pauseSimulation()
{
//timer->blockSignals(true);
...
}
but this time when I resume it restarts from zero, not from the time I have paused. :(
Re: How to use QTimer for pausing and resuming ?
Re: How to use QTimer for pausing and resuming ?
you mean before timer->start() in the resumeSimulation() method ?
It doens't work...
Re: How to use QTimer for pausing and resuming ?
Quote:
Originally Posted by
qt_user_154
but this time when I resume it restarts from zero, not from the time I have paused. :(
You need to adjust the way you calculate the time flow.
Re: How to use QTimer for pausing and resuming ?
Since it is a common pause, what sould I calculate ? I just want the timer to resume...
Re: How to use QTimer for pausing and resuming ?
Quote:
Originally Posted by
qt_user_154
Since it is a common pause, what sould I calculate ? I just want the timer to resume...
Do you want me to give you hints or to write a complete solution for you? :) In general you have to have some variable where you calculate the amount of time that has passed since the timer was initialized and increase that amount upon every timer tick.