Congratulations to our 2022 Qt Champions!

Timer instance connection delete

  • Hi

    I have the following to have a cert periodically updated:

    QTimer *SSLupdateTimer = new QTimer(this);
    connect(SSLupdateTimer, SIGNAL(timeout()), this, SLOT(UpdateSSLcertificate()));

    Usually this is called once. If this is called again, this will create a second timer, right? And it will also create another connection.

    How do I best catch this? How do I detect if there already is an instance and then kill it as well as the connection before creating a new one?

    ... or am I somehow wrong?

  • Lifetime Qt Champion


    Why not keep that timer as a private member of the class, null_ptr by default and check whether it's initialized when calling the function that contains that code ?

  • Lifetime Qt Champion

    Why do you create the timer instance on the heap?
    And if you want to do it like this, then put the pointer in your class and initialize the pointer with 0.
    Then each time your code is executed you check whether the pointer is null, if it is then you execute your code if not you do nothing.
    Do you delete the timer instance somewhere?

  • Thank you guys.
    I see that it was wrong to create it on the heap.
    I moved the creation and made it a private member of the class. When called, I check it's initialization and proceed accordingly.
    Works a treat.

Log in to reply