Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Timer question



  • How do I do to restart a QTimer of 1 minute in one function called PushButtonHandler each time I push a button, how do I stop the running timer, I cant use Timer->stop() before the declaration of the QTimer!!!!!!!, already tried if (isActive(&Timer)==TRUE ), does't work,Help please.
    @
    void MainWindow::PushButtonHandler()
    {
    do
    {
    if (seconds==0)
    seconds=10;

        //if (isActive(&Timer)==TRUE )?????????????????
        //    Timer->stop();?????????????????????
    
      // if (QTimer::isActive ())=TRUE)??????????????????
    
       //     QTimer::~QTimer ()); ????????????????
    
       t=t+5;
       seconds=seconds+5;
    }
    while (seconds>45);
    
    if (seconds==45)
      seconds=15;
    
    ui->lcdNumber_2->display(seconds);
    
    
    if (t>=6)
         {
             t--;
             QTimer *Timer = new QTimer(this);
             Timer->setInterval(36000);
             Timer->setSingleShot(true);
             connect(Timer, SIGNAL(timeout()), SLOT(Timer()));
             Timer->start();
          }
    

    }
    @


  • Moderators

    [Moderator's note: I split this into its own thread. Please start new threads for new questions, even if they're "sort of" related to an old thread.]

    But to answer your question, make the QTimer * a member of your class rather than a locally-created timer in the handler. (So you only create one timer.)

    Then you can just call theTimer->start() to start/restart it, or theTimer->stop() to stop it.

    Also, as a side note, never call a destructor explicitly! You'd want to use delete to delete a timer, if that's what you wanted to do.



  • Also: get rid of the loop in there. You do not need it.


Log in to reply