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. QTimer
Forum Updated to NodeBB v4.3 + New Features

QTimer

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.0k 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.
  • B Offline
    B Offline
    benjiii
    wrote on last edited by
    #1

    Hi

    Is it necessary to delete timer object after use in destructor.

    @
    //constructor--
    Class1::Class1()
    {
    pTimer = new QTimer();
    ........
    .........
    pTimer->start(1000);
    }

    //destructor
    Class1::~Class1()
    {
    delete pTimer;
    }

    @

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Serg
      wrote on last edited by
      #2

      Hello.
      Timer constructor: QTimer::QTimer (QObject * parent = 0);
      Use parent parameter and your class will automatically delete timer.
      @
      //constructor--
      Class1::Class1()
      {
      pTimer = new QTimer(this);
      ........
      .........
      pTimer->start(1000);
      }
      @

      It works If your class inherits from QObject. If not, then delete it in destructor.

      Also you can use member timer rather than pointer.
      @
      // header
      class Class1 {
      public:
      Class1();
      ~Class1();
      private:
      QTimer timer_;
      };

      // cpp
      Class1::Class1() : timer_()
      {
      ...
      ...
      timer_.start(1000);
      };
      @

      1 Reply Last reply
      0
      • B Offline
        B Offline
        benjiii
        wrote on last edited by
        #3

        Thanks Serg :)

        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