Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to pause and resume a Qtimer ?
QtWS25 Last Chance

How to pause and resume a Qtimer ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 8.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user_signed-up
    wrote on 26 Aug 2013, 09:48 last edited by
    #1

    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/library/html/qt4/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)

    @ // 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)`;
            minutes = (timeElapsed/60000)`;
            hours =  (timeElapsed/3600000)$;
            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.

    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 the updateTime() method.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Aug 2013, 10:02 last edited by
      #2

      Hi and welcome to devnet,

      You first problem is that you create a new timer each time you call startSimulation. You only need to create it once and re-use it.

      Also, why do you disconnect and reconnect pause ?

      Why use blockSignal on timer ? If you stop it, timeout should not get called

      Hope the questions will help

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • U Offline
        U Offline
        user_signed-up
        wrote on 27 Aug 2013, 10:04 last edited by
        #3

        Thanks, but how could I fix this out ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 27 Aug 2013, 10:26 last edited by
          #4

          Don't create a new timer in startSimulation. Create and setup your timer in the constructor

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          3/4

          27 Aug 2013, 10:04

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved