Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Help with QTimer scope
Forum Update on Monday, May 27th 2025

Help with QTimer scope

Scheduled Pinned Locked Moved Solved C++ Gurus
18 Posts 5 Posters 4.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.
  • C Offline
    C Offline
    Christianvs
    wrote on last edited by
    #1

    Hi

    I'm working with QT creator in a project and it seems like i'm stuck trying to get a QTimer to work.

    I have a MainWindow object in which i can create and use QTimers without any problems. Within the MainWindow i create an object, wc, of a class called weightChanger. Within wc i want a timer that connects to several slots within wc.

    I've tried a lot of different ways, but i can't seem to get the timers to connect. In my last try i tried declaring the timers in my header file for the weightChanger class:
    0_1520243899217_3949a7b8-b75b-4d45-a22f-2f9904ace37c-image.png

    Afterwards i initialize them in the constructor of wc and connects them aswell. I made a timerTest() slot which just sends a message to a console when executed, but i can't seem to get it working in any ways. I've tried several other ways of implementing the QTimer within the weightChanger class, but without any luck yet.
    0_1520243781379_5fb1d22d-193a-41bc-8852-50cd9952c4b1-image.png

    Anyone got a hint to what i got wrong?

    jsulmJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You should connect the signals before you start the timer. You set it for only 10 ms, so there is a chance that the timer will fire before your connection is made.

      (Z(:^

      1 Reply Last reply
      1
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        Also, make sure the connection succeeds (it returns a bool). And remember to declare timerTest() as a slot in your header file.

        Also, I heavily recommend using the new connection syntax, it's much better.

        (Z(:^

        aha_1980A 1 Reply Last reply
        3
        • sierdzioS sierdzio

          Also, make sure the connection succeeds (it returns a bool). And remember to declare timerTest() as a slot in your header file.

          Also, I heavily recommend using the new connection syntax, it's much better.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi @sierdzio,

          I was just about to write the same ;)

          @Christianvs please have a look here: https://wiki.qt.io/New_Signal_Slot_Syntax

          Qt has to stay free or it will die.

          1 Reply Last reply
          3
          • C Christianvs

            Hi

            I'm working with QT creator in a project and it seems like i'm stuck trying to get a QTimer to work.

            I have a MainWindow object in which i can create and use QTimers without any problems. Within the MainWindow i create an object, wc, of a class called weightChanger. Within wc i want a timer that connects to several slots within wc.

            I've tried a lot of different ways, but i can't seem to get the timers to connect. In my last try i tried declaring the timers in my header file for the weightChanger class:
            0_1520243899217_3949a7b8-b75b-4d45-a22f-2f9904ace37c-image.png

            Afterwards i initialize them in the constructor of wc and connects them aswell. I made a timerTest() slot which just sends a message to a console when executed, but i can't seem to get it working in any ways. I've tried several other ways of implementing the QTimer within the weightChanger class, but without any luck yet.
            0_1520243781379_5fb1d22d-193a-41bc-8852-50cd9952c4b1-image.png

            Anyone got a hint to what i got wrong?

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Christianvs To add to @sierdzio : why do you allocate both timers on the heap? Why not just:

            private:
                QTimer weightChangeTimer;
                QTimer changeWeightTimer;
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christianvs
              wrote on last edited by
              #6

              @sierdzio @aha_1980 @jsulm

              Thanks for the answers! I've tried following (i'm sorry about the messy code, in my attempt to get this working lines have been added, changed and commented out):

              0_1520248658951_72b4fec2-16da-4dc9-8bb0-e1d02793c340-image.png

              I am not sure if i get the new connect method right. In the case it should be the right way i connect it, i still seem to have the same problem as before.

              @jsulm
              I actually started out with initializing the Qtimers in the header file, but in my search for my mistake i tried allocating on the heap

              jsulmJ aha_1980A 2 Replies Last reply
              0
              • C Christianvs

                @sierdzio @aha_1980 @jsulm

                Thanks for the answers! I've tried following (i'm sorry about the messy code, in my attempt to get this working lines have been added, changed and commented out):

                0_1520248658951_72b4fec2-16da-4dc9-8bb0-e1d02793c340-image.png

                I am not sure if i get the new connect method right. In the case it should be the right way i connect it, i still seem to have the same problem as before.

                @jsulm
                I actually started out with initializing the Qtimers in the header file, but in my search for my mistake i tried allocating on the heap

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Christianvs What is ROS_INFO_STREAM?
                Can you try qDebug() instead to verify the slot is called?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                C 1 Reply Last reply
                1
                • C Christianvs

                  @sierdzio @aha_1980 @jsulm

                  Thanks for the answers! I've tried following (i'm sorry about the messy code, in my attempt to get this working lines have been added, changed and commented out):

                  0_1520248658951_72b4fec2-16da-4dc9-8bb0-e1d02793c340-image.png

                  I am not sure if i get the new connect method right. In the case it should be the right way i connect it, i still seem to have the same problem as before.

                  @jsulm
                  I actually started out with initializing the Qtimers in the header file, but in my search for my mistake i tried allocating on the heap

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Christianvs Are you sure your debug output works? Can you set a breakpoint in the slot?

                  Beside the "mess" your code looks Ok.

                  If it still not works, you have to look if you somewhere block signals or if the event loop is not running etc.

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Christianvs What is ROS_INFO_STREAM?
                    Can you try qDebug() instead to verify the slot is called?

                    C Offline
                    C Offline
                    Christianvs
                    wrote on last edited by Christianvs
                    #9

                    @jsulm

                    The whole project is connected through ROS as it controls an AC motor. To be honest i'm not sure if qDebug works in my current setup.

                    None the less i got the timer to say it's active by running isActive() after i start the timer (what a stupid mistake!)

                    But that actually solves part of my problem as i now know the timer is actually active!

                    But it still does not execute the timerTest() function!

                    jsulmJ 1 Reply Last reply
                    0
                    • C Christianvs

                      @jsulm

                      The whole project is connected through ROS as it controls an AC motor. To be honest i'm not sure if qDebug works in my current setup.

                      None the less i got the timer to say it's active by running isActive() after i start the timer (what a stupid mistake!)

                      But that actually solves part of my problem as i now know the timer is actually active!

                      But it still does not execute the timerTest() function!

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Christianvs said in Help with QTimer scope:

                      qDebug works in my current setup

                      std::cout should work.
                      And you can place a breakpoint inside the slot and start through debugger.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        Christianvs
                        wrote on last edited by
                        #11

                        Could the problem be that my weightChanger object somehow needs to be initialized in another way? By using new ?

                        0_1520249436236_6d85866a-a01e-4a81-b0ed-645520a5e3fa-image.png

                        Above is where i actually initialize the object and it is within this object the QTimer is stored

                        sierdzioS 1 Reply Last reply
                        0
                        • C Christianvs

                          Could the problem be that my weightChanger object somehow needs to be initialized in another way? By using new ?

                          0_1520249436236_6d85866a-a01e-4a81-b0ed-645520a5e3fa-image.png

                          Above is where i actually initialize the object and it is within this object the QTimer is stored

                          sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #12

                          @Christianvs said in Help with QTimer scope:

                          Could the problem be that my weightChanger object somehow needs to be initialized in another way? By using new ?

                          Yes! When this function ends, it will be destroyed (object goes out of scope).

                          (Z(:^

                          jsulmJ 1 Reply Last reply
                          2
                          • sierdzioS sierdzio

                            @Christianvs said in Help with QTimer scope:

                            Could the problem be that my weightChanger object somehow needs to be initialized in another way? By using new ?

                            Yes! When this function ends, it will be destroyed (object goes out of scope).

                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @sierdzio @Christianvs You can see this type of error very often here :-)

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • C Offline
                              C Offline
                              Christianvs
                              wrote on last edited by
                              #14

                              Yay! Now that i create it on the heap using new the timers work without any problems!

                              Thanks for the help all of you! :)

                              1 Reply Last reply
                              1
                              • sierdzioS Offline
                                sierdzioS Offline
                                sierdzio
                                Moderators
                                wrote on last edited by
                                #15

                                Just make sure it is cleaned up properly afterwards (either assign this as parent or call delete on it when you don't need it anymore).

                                (Z(:^

                                C 1 Reply Last reply
                                1
                                • Pablo J. RoginaP Offline
                                  Pablo J. RoginaP Offline
                                  Pablo J. Rogina
                                  wrote on last edited by
                                  #16

                                  @Christianvs if your issue is solved, please don't forget to mark your post as such. Thanks.

                                  Upvote the answer(s) that helped you solve the issue
                                  Use "Topic Tools" button to mark your post as Solved
                                  Add screenshots via postimage.org
                                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                  1 Reply Last reply
                                  0
                                  • sierdzioS sierdzio

                                    Just make sure it is cleaned up properly afterwards (either assign this as parent or call delete on it when you don't need it anymore).

                                    C Offline
                                    C Offline
                                    Christianvs
                                    wrote on last edited by Christianvs
                                    #17

                                    @sierdzio
                                    Is there any downside to just calling this as parent?

                                    @Pablo-J-Rogina
                                    I actually cannot find where to mark it as solved
                                    EDIT: Done! :)

                                    sierdzioS 1 Reply Last reply
                                    0
                                    • C Christianvs

                                      @sierdzio
                                      Is there any downside to just calling this as parent?

                                      @Pablo-J-Rogina
                                      I actually cannot find where to mark it as solved
                                      EDIT: Done! :)

                                      sierdzioS Offline
                                      sierdzioS Offline
                                      sierdzio
                                      Moderators
                                      wrote on last edited by
                                      #18

                                      @Christianvs said in Help with QTimer scope:

                                      Is there any downside to just calling this as parent?

                                      No. The other solution is worse, because it is easy to forget about calling delete. If you use Qt's parent-child system, then Qt will automatically delete objects when their parent is deleted.

                                      There is a possibility that it is not the behaviour you want, but that is not something we can determine from the code above.

                                      Other options for managing object life:

                                      • QSharedPointer
                                      • stl pointers

                                      (Z(:^

                                      1 Reply Last reply
                                      3

                                      • Login

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