Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTimer not calling slot
Qt 6.12 LTS is out! See what's new in the release blog

QTimer not calling slot

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 7 Posters 6.3k Views 6 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    And where are you calling initAll()?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    F 1 Reply Last reply
    0
    • F ForeverNoob

      Hi,

      I'm very new to Qt, trying now my first program. Having trouble with QTimer which doesn't call the slot function. This is the relevant code (inside MainWindow):

      int StatusTimerInterval = 2000;
      QTimer *statusTimer;
      .
      .
      void MainWindow::initAll(void)
      {
          .
          .
          statusTimer = new QTimer();
          connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));
          statusTimer->setInterval(StatusTimerInterval);
          statusTimer->start();
          .
          .
      }
      
      void MainWindow::updateStatus(void)
      {
          fprintf(stderr,"updateStatus\n");
      }
      

      updateStatus never executes. I stepped through the the initializing code and made sure the timer is active. What am I missing here?

      TIA

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #5

      @ForeverNoob said in QTimer not calling slot:

      statusTimer = new QTimer();

      Just a side note without looking at your actual issue :)

      Use this

      statusTimer = new QTimer(this);
      

      to make sure your QTimer (on heap) is included in your parent/child hierarchy and it gets cleaned up together with its parent (your mainWindow).


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      raven-worxR 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        And where are you calling initAll()?

        F Offline
        F Offline
        ForeverNoob
        wrote on last edited by
        #6

        @Christian-Ehrlicher said in QTimer not calling slot:

        And where are you calling initAll()?

        In MainWindow::MainWindow.

        1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @ForeverNoob said in QTimer not calling slot:

          statusTimer = new QTimer();

          Just a side note without looking at your actual issue :)

          Use this

          statusTimer = new QTimer(this);
          

          to make sure your QTimer (on heap) is included in your parent/child hierarchy and it gets cleaned up together with its parent (your mainWindow).

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #7

          @Pl45m4

          connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));

          incorrect SLOT usage (missing parenthesis).
          This is one of the reasons this old syntax is discouraged to be used.
          Use the "new" syntax with function pointers instead.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          Christian EhrlicherC F 2 Replies Last reply
          5
          • F ForeverNoob

            Hi,

            I'm very new to Qt, trying now my first program. Having trouble with QTimer which doesn't call the slot function. This is the relevant code (inside MainWindow):

            int StatusTimerInterval = 2000;
            QTimer *statusTimer;
            .
            .
            void MainWindow::initAll(void)
            {
                .
                .
                statusTimer = new QTimer();
                connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));
                statusTimer->setInterval(StatusTimerInterval);
                statusTimer->start();
                .
                .
            }
            
            void MainWindow::updateStatus(void)
            {
                fprintf(stderr,"updateStatus\n");
            }
            

            updateStatus never executes. I stepped through the the initializing code and made sure the timer is active. What am I missing here?

            TIA

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #8

            @ForeverNoob said in QTimer not calling slot:

            SLOT(updateStatus)

            Damn, just as @raven-worx has spotted! You would have to have written SLOT(updateStatus()) here. Which is why I said change over to new style syntax immediately, this is perfect reason, as @raven-worx says! :)

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @Pl45m4

              connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));

              incorrect SLOT usage (missing parenthesis).
              This is one of the reasons this old syntax is discouraged to be used.
              Use the "new" syntax with function pointers instead.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #9

              @raven-worx said in QTimer not calling slot:

              incorrect SLOT usage (missing parenthesis).

              cool, overseen this :)

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • raven-worxR raven-worx

                @Pl45m4

                connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));

                incorrect SLOT usage (missing parenthesis).
                This is one of the reasons this old syntax is discouraged to be used.
                Use the "new" syntax with function pointers instead.

                F Offline
                F Offline
                ForeverNoob
                wrote on last edited by
                #10

                @raven-worx said in QTimer not calling slot:

                @Pl45m4

                connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));

                incorrect SLOT usage (missing parenthesis).
                This is one of the reasons this old syntax is discouraged to be used.
                Use the "new" syntax with function pointers instead.

                Hi,

                I changed this statement to:

                connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus()));
                

                But that didn't help, updateStatus still wasn't executing.

                Then I tried the new syntax with something like this:

                connect(statusTimer, statusTimer->timeout(), this, &MainWindow::updateStatus);
                

                This wouldn't compile, QT Creator resdponds with an error message "too few arguments to function call, expected 1, have 0". I'm not sure which argument is missing, prsumably in the timeout() function. Couldn't find any example anywhere.

                JKSHJ 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  Hi,

                  Remove the void from your slot argument list and try to build again.

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

                  F JonBJ 2 Replies Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    Remove the void from your slot argument list and try to build again.

                    F Offline
                    F Offline
                    ForeverNoob
                    wrote on last edited by
                    #12

                    @SGaist said in QTimer not calling slot:

                    Hi,

                    Remove the void from your slot argument list and try to build again.

                    OK, I forgot to add the function to the slot list. Everything works now. Thanks a lot to all who helped.

                    raven-worxR 1 Reply Last reply
                    0
                    • F ForeverNoob

                      @raven-worx said in QTimer not calling slot:

                      @Pl45m4

                      connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));

                      incorrect SLOT usage (missing parenthesis).
                      This is one of the reasons this old syntax is discouraged to be used.
                      Use the "new" syntax with function pointers instead.

                      Hi,

                      I changed this statement to:

                      connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus()));
                      

                      But that didn't help, updateStatus still wasn't executing.

                      Then I tried the new syntax with something like this:

                      connect(statusTimer, statusTimer->timeout(), this, &MainWindow::updateStatus);
                      

                      This wouldn't compile, QT Creator resdponds with an error message "too few arguments to function call, expected 1, have 0". I'm not sure which argument is missing, prsumably in the timeout() function. Couldn't find any example anywhere.

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #13

                      @ForeverNoob said in QTimer not calling slot:

                      connect(statusTimer, statusTimer->timeout(), this, &MainWindow::updateStatus);
                      

                      That should also be

                      connect(statusTimer, &QTimer::timeout, this, &MainWindow::updateStatus);

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      F 1 Reply Last reply
                      2
                      • JKSHJ JKSH

                        @ForeverNoob said in QTimer not calling slot:

                        connect(statusTimer, statusTimer->timeout(), this, &MainWindow::updateStatus);
                        

                        That should also be

                        connect(statusTimer, &QTimer::timeout, this, &MainWindow::updateStatus);

                        F Offline
                        F Offline
                        ForeverNoob
                        wrote on last edited by
                        #14

                        @JKSH said in QTimer not calling slot:

                        connect(statusTimer, &QTimer::timeout, this, &MainWindow::updateStatus);

                        That works fine. Many thanks.

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Hi,

                          Remove the void from your slot argument list and try to build again.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #15

                          @SGaist said in QTimer not calling slot:

                          Remove the void from your slot argument list and try to build again.

                          @ForeverNoob
                          I had not noticed you had written void MainWindow::updateStatus(void). The same applies to your void MainWindow::initAll(void). And presumably any other functions you have defined in a similar fashion. Unlike C, in C++, where declaring arguments to functions is mandatory, we don't write function(void) to indicate no parameters, we just write function().

                          1 Reply Last reply
                          0
                          • F ForeverNoob

                            @SGaist said in QTimer not calling slot:

                            Hi,

                            Remove the void from your slot argument list and try to build again.

                            OK, I forgot to add the function to the slot list. Everything works now. Thanks a lot to all who helped.

                            raven-worxR Offline
                            raven-worxR Offline
                            raven-worx
                            Moderators
                            wrote on last edited by
                            #16

                            @ForeverNoob said in QTimer not calling slot:

                            OK, I forgot to add the function to the slot list. Everything works now. Thanks a lot to all who helped.

                            with the function pointer syntax the slots do not have to be declared as slots anymore.
                            Only if you want to call them via the meta-object system (but thats a different story)

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            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
                            • Unsolved