QTimer



  • 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



  • 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);
    };
    @



  • Thanks Serg :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.