Use QTimer for the time part.
For a โscreen saver,โ for example, create a timer with one shot, connect it to your custom slot and set the interval to two minutes.
activeTimer = new QTimer(this); activeTimer->setInterval(2*60*1000); activeTimer->setSingleShot(true); connect(activeTimer, SIGNAL(timeout()), this, SLOT(activateAutoClick())); activeTimer->start();
In this user slot, run the second non-single snapshot timer connected to the second user slot
void YourThing::activateAutoClick() { autoTimer->setInterval(5*1000); autoTimer->setSingleShot(false); connect(autoTimer, SIGNAL(timeout()), this, SLOT(autoClick())); autoTimer->start(); }
And do everything you need in terms of sending signals to your port in autoClick .
To cancel any timer, simply call their stop() / slot method.
To implement the screen saver behavior, create a function that:
- Call
autoTimer->stop() to turn off automatic clicks - Call
activeTimerr->start(2*60*1000) to restart this
And call this function when necessary. You can do this from existing slots for your buttons or redefine event handlers like QWidget mouseMoveEvent , keyPressedEvent , etc. (Be sure to read the documentation for the handlers; some require special preparation.)
source share