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. Documentation suggesting implementing QTimer as a pointer, why is that?
Qt 6.11 is out! See what's new in the release blog

Documentation suggesting implementing QTimer as a pointer, why is that?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.6k 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    In the official documentation, we are given this piece of sample code, where a QTimer is declared as a pointer.

        QTimer *timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
        timer->start(1000);
    

    Why does QTimer need to be declared as a pointer? Are there any known issues with declaring QTimer as an object, like so?

    QTimer timer;
    
    JonBJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      In the official documentation, we are given this piece of sample code, where a QTimer is declared as a pointer.

          QTimer *timer = new QTimer(this);
          connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
          timer->start(1000);
      

      Why does QTimer need to be declared as a pointer? Are there any known issues with declaring QTimer as an object, like so?

      QTimer timer;
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Dummie1138
      You can use a stack QTimer timer instead of a heap one if you wish. But you must then look at what its scope/lifetime is. We spend a lot of time here with people using stack objects which go out of scope when they don't intend them to....

      Dummie1138D 1 Reply Last reply
      2
      • JonBJ JonB

        @Dummie1138
        You can use a stack QTimer timer instead of a heap one if you wish. But you must then look at what its scope/lifetime is. We spend a lot of time here with people using stack objects which go out of scope when they don't intend them to....

        Dummie1138D Offline
        Dummie1138D Offline
        Dummie1138
        wrote on last edited by
        #3

        @JonB So, is using a heap timer just for the sake of having it placed on the heap? My application of this timer should be under 20 seconds so I'm assuming placing it on the stack for me is not going to be too big of an issue.

        J.HilkJ jsulmJ 2 Replies Last reply
        0
        • Dummie1138D Dummie1138

          @JonB So, is using a heap timer just for the sake of having it placed on the heap? My application of this timer should be under 20 seconds so I'm assuming placing it on the stack for me is not going to be too big of an issue.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Dummie1138 said in Documentation suggesting implementing QTimer as a pointer, why is that?:

          My application of this timer should be under 20 seconds so I'm assuming placing it on the stack for me is not going to be too big of an issue

          alt text

          use heap.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • Dummie1138D Dummie1138

            @JonB So, is using a heap timer just for the sake of having it placed on the heap? My application of this timer should be under 20 seconds so I'm assuming placing it on the stack for me is not going to be too big of an issue.

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

            @Dummie1138 said in Documentation suggesting implementing QTimer as a pointer, why is that?:

            for me is not going to be too big of an issue

            Depending on what exactly you are doing (where you are declaring the QTimer actually?) you may be wrong.
            What do you think how long does the timer exist in the code bellow:

            void MyClass::myMethod()
            {
                QTimer timer;
                // Do something
            }
            

            And how long does it exist bellow:

            void MyClass::myMethod()
            {
                QTimer *timer = new QTimer();
                // Do something
            }
            

            Do you see the difference?

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

            1 Reply Last reply
            6

            • Login

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