ProEns08 ProEns08 - 1 year ago 77
C++ Question

Qt how to make a button responsive when the slot connected to its clicked() signal is running?

I have a button with a slot connected to its clicked() signal.

The slot is a time consuming task and take many seconds.

When trying to re-click the button before the slot ends, I can't.

How to make the button responsive when running the slot connected to its clicked() signal?

Answer Source

You have two high-level choices:

  • spawn the time-consuming choices into a thread: the simplest way is to use QtConcurrent::run(), and on the GUI side you'll need to worry about handling all the GUI events appropriately while your operation executes.
  • within your time-consuming code, call QApplication::processEvents() often enough that the GUI remains responsive (e.g., on the inside of a loop): this won't work if the time-consuming operation is 3rd party code you can't break out of, and you'll still need to work out how to handle GUI operations while you've got your long-running task executing.

You'll find that the first choice is by far the consensus better choice over the second, FWIW.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download