Detecting whether a singleshot QTimer is currently being run
-
Hi. I have a function that starts a singleshot QTimer for 5 seconds. During those 5 seconds, there are a series of functions that I want to behave differently.
The way I have it executed right now is like so:
bool isSingleShotActive; void functionName(){ //Some other function processes QTimer::singleShot(5000, this, SLOT(otherFunctionName())); //triggers the determineStability function after 5 seconds. isSingleShotActive = true; } void otherFunctionName(){ //Some other function processes isSingleShotActive = false; } void evenOtherFunctionName(){ if (isSingleShotActive == false) do_the_special_thing; else do_the_default_thing; }
I was wondering whether there is a signal I can use on the oneshot to indicate that the oneshot is active.
-
@Dummie1138
static QTimer::singleShot()
is really just a "convenience" function. It simply creates/uses aQTimer
instance of its own, which you cannot see/access.Yes, the code you show is indeed the correct/corresponding way to create your own
QTimer
instance and set it to single-shot mode. If you want to access thatQTimer
variable from elsewhere you will need to e.g. make it a class member variable, and an accessor method if you want it accessible outside the class. -
You could create your instance of QTimer on the heap and then use QTimer::isActive() in your other method.
-
@mchinand Are you suggesting building a QTimer and changing singleShot to true instead of using QTimer::singleShot? My understanding of QTimer::singleShot is that it's just a function and not a QTimer object, is that correct?
Something like this:
QTimer *timer = new QTimer(this); timer->setSingleShot(true); connect(timer, &QTimer::timeout, this, SLOT(otherFunctionName())); timer->start(5000);
In that case will I have to declare QTimer *timer outside of the function, given that I need it's existence to be detected by another function?
-
@Dummie1138
static QTimer::singleShot()
is really just a "convenience" function. It simply creates/uses aQTimer
instance of its own, which you cannot see/access.Yes, the code you show is indeed the correct/corresponding way to create your own
QTimer
instance and set it to single-shot mode. If you want to access thatQTimer
variable from elsewhere you will need to e.g. make it a class member variable, and an accessor method if you want it accessible outside the class.