Destroying/disconnecting a QTimer initialized in another function
-
Hi. I have a QTimer on the heap that is created every time a certain function is called. I would like to make sure I only have one of these QTimers by destroying the previous QTimer every time at the start of that certain function being called. This is how the Timer is made:
void ApplicationSettings::on_combo_changeBar_activated(int index){ QTimer *timer = new QTimer(this); int seconds; switch (index){ //Written for redundancy. Future values are probably not gonna be in 2s. case 0: seconds = 1; break; case 1: seconds = 2; break; case 2: seconds = 4; break; case 3: seconds = 6; break; case 4: seconds = 8; break; default: seconds = 2; } qDebug() << seconds; ui->progressBar->setValue(0); connect(timer, &QTimer::timeout, this, [=](){this->otherFunction();}); timer->start(seconds*10); }How can I destroy this QTimer the next time this function is called, such that there will only be one at all times? I wasn't able to find any documentation in the QTimer documentation.
-
Have your timer object be a private member of your class and then do:
void ApplicationSettings::on_combo_changeBar_activated(int index){ if (timer == nullptr){// or instantiate it in the constructor timer = new QTimer(); connect(timer, &QTimer::timeout, this, [=](){this->otherFunction();}); } if (timer->isActive()){ timer->stop(); } . . . timer->start(seconds*10); } -
Have your timer object be a private member of your class and then do:
void ApplicationSettings::on_combo_changeBar_activated(int index){ if (timer == nullptr){// or instantiate it in the constructor timer = new QTimer(); connect(timer, &QTimer::timeout, this, [=](){this->otherFunction();}); } if (timer->isActive()){ timer->stop(); } . . . timer->start(seconds*10); }@mchinand Thank you very much for the response. I am aware that I can declare my timer as part of my class, but I was wondering whether there is a way to simply destroy the timer when the function is redeclared.
This function is meant to be declared every 20-80 mills, btw.
-
@mchinand Thank you very much for the response. I am aware that I can declare my timer as part of my class, but I was wondering whether there is a way to simply destroy the timer when the function is redeclared.
This function is meant to be declared every 20-80 mills, btw.
@Dummie1138 said in Destroying/disconnecting a QTimer initialized in another function:
when the function is redeclared.
What does this mean?
-
@mchinand Thank you very much for the response. I am aware that I can declare my timer as part of my class, but I was wondering whether there is a way to simply destroy the timer when the function is redeclared.
This function is meant to be declared every 20-80 mills, btw.
@Dummie1138 said in Destroying/disconnecting a QTimer initialized in another function:
whether there is a way to simply destroy the timer when the function is redeclared.
As @Christian-Ehrlicher says, we do not know what "redeclared" means here.
Assuming you mean when this function is called again. To destroy (or reset) the timer in the function you will need a variable which refers to the timer. You cannot have a function local variable retain its value between calls (don't use
statichere). So you will need to store that reference variable outside the function somewhere. That is why it would need to be a class variable.