How to emit signals with an interval in Qt?

I am writing a simple port communications program. On the GUI side of the application, I have a panel with 12 buttons that send signals to the parallel port interface. Communication with the port is up and running. Now I need to automatically switch between the buttons. The goal is to launch a screensaver view that will periodically activate buttons and send signals to the port. In practice, it will look like this: the timer starts within 2 minutes, and if any event occurs, it restarts. Otherwise, if the timer reaches timeout() , a qt signal is issued, switching starts and the buttons automatically click() 'ed with an interval of 5 seconds.

My questions:

  • How to enable the start timer, which will be reset if an event occurs with the key / mouse?
  • How to determine the transition between the buttons with an interval of standby?
+4
source share
1 answer

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.)

+6
source

All Articles