Results 1 to 8 of 8

Thread: How to use QTimer for pausing and resuming ?

  1. #1
    Join Date
    Aug 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Question 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)

    Qt Code:
    1. // Creation of the Buttons and the time area
    2. void MainWindow::createBottom()
    3. {
    4.  
    5. bottom = new QWidget();
    6.  
    7. play = new QPushButton("Launch",this);
    8. pause = new QPushButton("Pause",this);
    9. play->setDisabled(false);
    10. pause->setDisabled(true);
    11. timeEdit = new QTimeEdit(this);
    12. timeEdit->setDisplayFormat("mm:ss");
    13.  
    14. layout->addWidget(play);
    15. layout->addWidget(pause);
    16. layout->addWidget(timeEdit );
    17. bottom->setLayout(layout);
    18.  
    19. connect(play, SIGNAL(clicked()), this, SLOT(startSimulation()));
    20. connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
    21. }
    22.  
    23. // to resume the timer where is was stopped
    24. void MainWindow::resumeSimulation()
    25. {
    26. timer->blockSignals( false );
    27. pause->setText("Pause");
    28. pause->disconnect(SIGNAL(clicked()));
    29. connect(pause, SIGNAL(clicked()), this, SLOT(pauseSimulation()));
    30. paused = false;
    31.  
    32. timer->start();
    33. int timeOfPause = time->restart();
    34. int timeTotal = timeOfPause + timeElapsed;
    35. time->addMSecs(-timeTotal);
    36.  
    37. }
    38.  
    39. // to Start the timer
    40. void MainWindow::pauseSimulation()
    41. {
    42. timer->blockSignals(true);
    43. pause->setText("Resume");
    44. timer->stop();
    45. play->setDisabled(false);
    46. //pause->setDisabled(true);
    47. pause->disconnect(SIGNAL(clicked()));
    48.  
    49. connect(pause, SIGNAL(clicked()), this, SLOT(resumeSimulation()));
    50. paused = true;
    51. }
    52.  
    53. // to Start the timer from zero.
    54. void MainWindow::startSimulation()
    55. {
    56.  
    57. timer = new QTimer(this);
    58. connect(timer, SIGNAL(timeout()), this , SLOT(updateTime()));
    59. timer->start(500);
    60. play->setDisabled(true);
    61.  
    62. pause->setDisabled(false);
    63. }
    64. void MainWindow::updateTime()
    65. {
    66. if(time == NULL)
    67. {
    68. time = new QTime(0,0,0,0);
    69. time->start();
    70. }
    71. //timeEdit->setTime(QTime::fromS(time->elapsed()));
    72. //time = &(time->addMSecs(1000));
    73. if(hasRestart)
    74. {
    75. time->restart();
    76. time->addMSecs(-timeElapsed);
    77. hasRestart = false;
    78. }
    79. else
    80. {
    81. timeElapsed =+ time->elapsed();
    82. }
    83. int seconds = 0;
    84. int minutes = 0;
    85. int hours = 0;
    86.  
    87. if(!paused)
    88. {
    89. seconds = (timeElapsed/1000)%60;
    90. minutes = (timeElapsed/60000)%60;
    91. hours = (timeElapsed/3600000)%24;
    92. std::cout << "Test : " << hours << ":" << minutes << ":" << seconds << std::endl;
    93. timeEdit->setTime(QTime(0,minutes,seconds,0));
    94. timeEdit->update();
    95. }
    96. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by qt_user_154; 26th August 2013 at 12:28.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use QTimer for pausing and resuming ?

    Thanks wysota I have commented the lines :

    Qt Code:
    1. void MainWindow::resumeSimulation()
    2. {
    3. //timer->blockSignals( false );
    4. ...
    5. }
    6.  
    7. void MainWindow::pauseSimulation()
    8. {
    9. //timer->blockSignals(true);
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    but this time when I resume it restarts from zero, not from the time I have paused.

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QTimer for pausing and resuming ?

    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Aug 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use QTimer for pausing and resuming ?

    you mean before timer->start() in the resumeSimulation() method ?

    It doens't work...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QTimer for pausing and resuming ?

    Quote Originally Posted by qt_user_154 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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...

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QTimer for pausing and resuming ?

    Quote Originally Posted by qt_user_154 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 19:47
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. Problem pausing the main thread
    By franco.amato in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2011, 21:47
  4. Pausing a thread while waiting that phonon play a sound file
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2011, 21:41
  5. QTimer
    By dragon in forum Qt Programming
    Replies: 18
    Last Post: 16th November 2008, 14:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.