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.
  • 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 Offline
      jsulmJ Offline
      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 Offline
            jsulmJ Offline
            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