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 kill a timer

How to kill a timer

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.5k 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.
  • D Offline
    D Offline
    Driftwood
    wrote on 26 Aug 2021, 16:07 last edited by Driftwood
    #1

    I have this code:

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(scrollKeeper()));
    timer->start(80);
    

    As you can see, it triggers scrollKeeper() function where the timer continues to run. And run and run and run. I need to be able to kill it in scrollKeeper(), but I can't seem to make that happen.

    Is there a way to get the timer's ID so i can use killTimer(ID)? Or will that even work with timer->start()?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 26 Aug 2021, 16:11 last edited by JoeCFD
      #2
      if ( nullptr == timer ) {
          **** output error msg *****
      }
      else {
         if ( timer->isActive() )  
         {
             timer->stop(); 
         }
      }
      
      D 1 Reply Last reply 26 Aug 2021, 16:42
      2
      • J JoeCFD
        26 Aug 2021, 16:11
        if ( nullptr == timer ) {
            **** output error msg *****
        }
        else {
           if ( timer->isActive() )  
           {
               timer->stop(); 
           }
        }
        
        D Offline
        D Offline
        Driftwood
        wrote on 26 Aug 2021, 16:42 last edited by
        #3

        @JoeCFD

        I tried that last night and crashed. I guess I have this wired up all wrong, even after reading the docs and viewing a few examples online.

        I'll keep on it, though. Thanks for your input.

        J J 2 Replies Last reply 26 Aug 2021, 16:54
        0
        • D Driftwood
          26 Aug 2021, 16:42

          @JoeCFD

          I tried that last night and crashed. I guess I have this wired up all wrong, even after reading the docs and viewing a few examples online.

          I'll keep on it, though. Thanks for your input.

          J Offline
          J Offline
          JoeCFD
          wrote on 26 Aug 2021, 16:54 last edited by
          #4

          @Driftwood run your code in debugger and you can easily find the problem. call stop() should not crash your app.

          1 Reply Last reply
          0
          • D Driftwood
            26 Aug 2021, 16:42

            @JoeCFD

            I tried that last night and crashed. I guess I have this wired up all wrong, even after reading the docs and viewing a few examples online.

            I'll keep on it, though. Thanks for your input.

            J Offline
            J Offline
            JonB
            wrote on 26 Aug 2021, 16:57 last edited by JonB
            #5

            @Driftwood said in How to kill a timer:

            I tried that last night and crashed

            It would crash if timer == nullptr, or doesn't point to a valid instance for whatever reason. What is the scope of your QTimer *timer = new QTimer(this);, because that's the timer-> you will need, and it doesn't look like a class member variable?

            J D 2 Replies Last reply 26 Aug 2021, 17:03
            1
            • J JonB
              26 Aug 2021, 16:57

              @Driftwood said in How to kill a timer:

              I tried that last night and crashed

              It would crash if timer == nullptr, or doesn't point to a valid instance for whatever reason. What is the scope of your QTimer *timer = new QTimer(this);, because that's the timer-> you will need, and it doesn't look like a class member variable?

              J Offline
              J Offline
              JoeCFD
              wrote on 26 Aug 2021, 17:03 last edited by
              #6

              @JonB Good point! timer should be defined in the header.

              1 Reply Last reply
              0
              • J JonB
                26 Aug 2021, 16:57

                @Driftwood said in How to kill a timer:

                I tried that last night and crashed

                It would crash if timer == nullptr, or doesn't point to a valid instance for whatever reason. What is the scope of your QTimer *timer = new QTimer(this);, because that's the timer-> you will need, and it doesn't look like a class member variable?

                D Offline
                D Offline
                Driftwood
                wrote on 26 Aug 2021, 17:06 last edited by Driftwood
                #7

                @JonB

                snippet of mainwindow.cpp

                void MainWindow::scrollKeeper()
                {
                    ui->label_About->move(380, g_counter);
                    g_counter -= 1;
                    if (g_counter < 100) {
                        timer->stop();
                        return;
                    }
                    if (g_counter < -(ui->label_About->height()))
                        g_counter = ui->stackedWidget->height()+6;
                //    qDebug()<< g_counter;
                }
                
                
                void MainWindow::on_action_About_triggered()
                {
                    ui->stackedWidget->setCurrentIndex(2);
                    ui->label_About->move(380, ui->stackedWidget->height());
                
                    g_counter = ui->stackedWidget->height()+6;
                //    QTimer *timer = new QTimer(this);
                    connect(timer, SIGNAL(timeout()), this, SLOT(scrollKeeper()));
                    timer->start(30);
                }
                

                snippet of the header file:

                private:
                    Ui::MainWindow *ui;
                
                    int g_counter;
                
                    QTimer *timer;
                

                In the second function up there, it crashes at timer->start(30); with this error: QObject::connect: Cannot connect (null)::timeout() to MainWindow::scrollKeeper()

                A for loop will work, if need be. And it's looking more and more that way =)

                J 1 Reply Last reply 26 Aug 2021, 17:19
                0
                • D Driftwood
                  26 Aug 2021, 17:06

                  @JonB

                  snippet of mainwindow.cpp

                  void MainWindow::scrollKeeper()
                  {
                      ui->label_About->move(380, g_counter);
                      g_counter -= 1;
                      if (g_counter < 100) {
                          timer->stop();
                          return;
                      }
                      if (g_counter < -(ui->label_About->height()))
                          g_counter = ui->stackedWidget->height()+6;
                  //    qDebug()<< g_counter;
                  }
                  
                  
                  void MainWindow::on_action_About_triggered()
                  {
                      ui->stackedWidget->setCurrentIndex(2);
                      ui->label_About->move(380, ui->stackedWidget->height());
                  
                      g_counter = ui->stackedWidget->height()+6;
                  //    QTimer *timer = new QTimer(this);
                      connect(timer, SIGNAL(timeout()), this, SLOT(scrollKeeper()));
                      timer->start(30);
                  }
                  

                  snippet of the header file:

                  private:
                      Ui::MainWindow *ui;
                  
                      int g_counter;
                  
                      QTimer *timer;
                  

                  In the second function up there, it crashes at timer->start(30); with this error: QObject::connect: Cannot connect (null)::timeout() to MainWindow::scrollKeeper()

                  A for loop will work, if need be. And it's looking more and more that way =)

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 26 Aug 2021, 17:19 last edited by JoeCFD
                  #8

                  @Driftwood
                  private:
                  Ui::MainWindow *m_ui{};
                  int m_gCounter{};
                  QTimer *m_timer{};

                  m_timer = new QTimer(this); ===>wrong: QTimer *timer = new QTimer(this);

                  D 1 Reply Last reply 26 Aug 2021, 17:34
                  2
                  • J JoeCFD
                    26 Aug 2021, 17:19

                    @Driftwood
                    private:
                    Ui::MainWindow *m_ui{};
                    int m_gCounter{};
                    QTimer *m_timer{};

                    m_timer = new QTimer(this); ===>wrong: QTimer *timer = new QTimer(this);

                    D Offline
                    D Offline
                    Driftwood
                    wrote on 26 Aug 2021, 17:34 last edited by
                    #9

                    @JoeCFD
                    Thank you. I see what I did wrong (more than one thing, really). It works nicely now. Before I decided I wanted the text to stop at a certain y axis, I had it scrolling nicely. I didn't realize the text skidding to a stop wld be so monumental to me.

                    But one last thing: Is there a way to get a timer's ID, or is that even necessary?

                    J 1 Reply Last reply 26 Aug 2021, 17:54
                    0
                    • D Driftwood
                      26 Aug 2021, 17:34

                      @JoeCFD
                      Thank you. I see what I did wrong (more than one thing, really). It works nicely now. Before I decided I wanted the text to stop at a certain y axis, I had it scrolling nicely. I didn't realize the text skidding to a stop wld be so monumental to me.

                      But one last thing: Is there a way to get a timer's ID, or is that even necessary?

                      J Offline
                      J Offline
                      JoeCFD
                      wrote on 26 Aug 2021, 17:54 last edited by
                      #10

                      @Driftwood timerId(); But what is for?

                      D 1 Reply Last reply 26 Aug 2021, 18:32
                      0
                      • J JoeCFD
                        26 Aug 2021, 17:54

                        @Driftwood timerId(); But what is for?

                        D Offline
                        D Offline
                        Driftwood
                        wrote on 26 Aug 2021, 18:32 last edited by Driftwood
                        #11

                        @JoeCFD

                        I was just curious about how to get a timer's ID. This is my first time to use QTimer. I read the docs and looked at a few examples and i still messed it up.

                        You've been a great help. Thanks again.

                        mrjjM 1 Reply Last reply 26 Aug 2021, 18:37
                        0
                        • D Driftwood
                          26 Aug 2021, 18:32

                          @JoeCFD

                          I was just curious about how to get a timer's ID. This is my first time to use QTimer. I read the docs and looked at a few examples and i still messed it up.

                          You've been a great help. Thanks again.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 26 Aug 2021, 18:37 last edited by
                          #12

                          @Driftwood

                          Hi
                          https://doc.qt.io/qt-5/qtimer.html#timerId

                          1 Reply Last reply
                          0

                          1/12

                          26 Aug 2021, 16:07

                          • Login

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