Stop timer after 30 seconds
-
Hi. I have made a QTimer that fires once every second.
QTimer *requestSetptTimer = new QTimer(this); connect(requestSetptTimer, SIGNAL(timeout()), this, SLOT(requestSetPoint())); connect(this, SIGNAL(reachedStableSetPoint()), requestSetptTimer, SLOT(stop())); //Disconnect on timeout or stable. requestSetptTimer->start(1000); }Right now it stops after a signal, "reachedStableSetPoint", is emitted. I would also like it to stop firing if reachedStableSetPoint is not emitted, but 30 seconds have passed. So far I have the following code.
QTimer *requestSetptTimer = new QTimer(this); connect(requestSetptTimer, SIGNAL(timeout()), this, SLOT(requestSetPoint())); connect(this, SIGNAL(reachedStableSetPoint()), requestSetptTimer, SLOT(stop())); //Disconnect on timeout or stable. QTimer::singleShot(30000, requestSetptTimer, SLOT(stop())); requestSetptTimer->start(1000);I was wondering whether this is the standard approach, and if not, what the standard approach would be. Please let me know if more info is required.
-
Hi. I have made a QTimer that fires once every second.
QTimer *requestSetptTimer = new QTimer(this); connect(requestSetptTimer, SIGNAL(timeout()), this, SLOT(requestSetPoint())); connect(this, SIGNAL(reachedStableSetPoint()), requestSetptTimer, SLOT(stop())); //Disconnect on timeout or stable. requestSetptTimer->start(1000); }Right now it stops after a signal, "reachedStableSetPoint", is emitted. I would also like it to stop firing if reachedStableSetPoint is not emitted, but 30 seconds have passed. So far I have the following code.
QTimer *requestSetptTimer = new QTimer(this); connect(requestSetptTimer, SIGNAL(timeout()), this, SLOT(requestSetPoint())); connect(this, SIGNAL(reachedStableSetPoint()), requestSetptTimer, SLOT(stop())); //Disconnect on timeout or stable. QTimer::singleShot(30000, requestSetptTimer, SLOT(stop())); requestSetptTimer->start(1000);I was wondering whether this is the standard approach, and if not, what the standard approach would be. Please let me know if more info is required.
@Dummie1138 AFAIK its either this, or starting a QElapsedTimer and checking that each execution of your requestSetPoint function
-
Hello!
You can make it using just a
QTimerand a counter variable. Please, check out my code below:Code:
int i = 0; QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, [this, timer, i]() mutable { qDebug() << "Do something: " << i; if (i == 30) { qDebug() << "Timout!"; timer->stop(); timer->deleteLater(); } i++; }); timer->start(1000);It runs every second. When 30 seconds have passed it displays
"Timout!"and stops. Happy coding! -
Hello!
You can make it using just a
QTimerand a counter variable. Please, check out my code below:Code:
int i = 0; QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, [this, timer, i]() mutable { qDebug() << "Do something: " << i; if (i == 30) { qDebug() << "Timout!"; timer->stop(); timer->deleteLater(); } i++; }); timer->start(1000);It runs every second. When 30 seconds have passed it displays
"Timout!"and stops. Happy coding!@Cobra91151
Yes, but not sure I would use this if I need that 30 seconds to be "accurate", it could exceed that more in 30 calls of timeout than one single shot or elapsed timer. Just saying, don't know if that would be an issue in practice. -
@Cobra91151
Yes, but not sure I would use this if I need that 30 seconds to be "accurate", it could exceed that more in 30 calls of timeout than one single shot or elapsed timer. Just saying, don't know if that would be an issue in practice.Hello!
Yes, you are right reqarding
QElapsedTimer, it could be more "accurate".Code:
QElapsedTimer *timer = new QElapsedTimer; timer->start(); while (!timer->hasExpired(30000)) { qDebug() << "Elapsed: " << timer->elapsed() << " milliseconds"; } delete timer;Or usage of
QDeadlineTimerwhich counts towards a timeout in the future instead of tracking elapsed time.QDeadlineTimer timer(30000); while (!timer.hasExpired()) { qDebug() << "Remaining time: " << timer.remainingTime(); }It also could be useful solution in this case, but running 2 QTimers and one QTimer runs only to stop another I do not think it would be a good practice.
-
D Dummie1138 has marked this topic as solved on