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
Qt 6.11 is out! See what's new in the release blog

How to kill a timer

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 4.2k Views 1 Watching
  • 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.
  • JoeCFDJ JoeCFD
    if ( nullptr == timer ) {
        **** output error msg *****
    }
    else {
       if ( timer->isActive() )  
       {
           timer->stop(); 
       }
    }
    
    DriftwoodD Offline
    DriftwoodD Offline
    Driftwood
    wrote on 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.

    JoeCFDJ JonBJ 2 Replies Last reply
    0
    • DriftwoodD Driftwood

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

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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
      • DriftwoodD Driftwood

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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?

        JoeCFDJ DriftwoodD 2 Replies Last reply
        1
        • JonBJ JonB

          @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?

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #6

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

          1 Reply Last reply
          0
          • JonBJ JonB

            @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?

            DriftwoodD Offline
            DriftwoodD Offline
            Driftwood
            wrote on 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 =)

            JoeCFDJ 1 Reply Last reply
            0
            • DriftwoodD Driftwood

              @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 =)

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on 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);

              DriftwoodD 1 Reply Last reply
              2
              • JoeCFDJ JoeCFD

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

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

                DriftwoodD Offline
                DriftwoodD Offline
                Driftwood
                wrote on 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?

                JoeCFDJ 1 Reply Last reply
                0
                • DriftwoodD Driftwood

                  @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?

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #10

                  @Driftwood timerId(); But what is for?

                  DriftwoodD 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @Driftwood timerId(); But what is for?

                    DriftwoodD Offline
                    DriftwoodD Offline
                    Driftwood
                    wrote on 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
                    0
                    • DriftwoodD Driftwood

                      @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 last edited by
                      #12

                      @Driftwood

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

                      1 Reply Last reply
                      0

                      • Login

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