1

A typical line I often need is

connect(thread, SIGNAL(started()), this->actExecute, SLOT(setDisabled(>>bool<<)));

But afaik you can't connect signals/slots with different signatures.

It's often recommended to use a helper method to trigger/emit a custom signal which passes true/false, but I have a lot of actions which needs to get disabled/enabled on thread start/stop/other, so I want to avoid writing dozens of helper functions/signals.

Is there a better way (maybe a single-line solution)?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
cytrinox
  • 1,846
  • 5
  • 25
  • 46

3 Answers3

5

There is a way to do this but it depends on Qt 5 and C++11. You can use a lambda expression to call the slot with the appropriate parameters. There was recently a question asking how to do this specifically with QTimers, here is a link to the answer I gave earlier.

Basically though, the code will look something like this-

connect(thread, &QThread::started, this->actExecute, [=]() {
    setDisabled(true);
  } );
Community
  • 1
  • 1
Linville
  • 3,613
  • 1
  • 27
  • 41
1

Make your own Action class by deriving it from QAction and add simple disable() and enable() slots to it.

0

If Qt 5 and C++11 can't be used, there are 2 possible solutions for this problem:

1) Derive QAction

Derive QAction and add 2 simple slots(disabled/enabled) to it as @Roku said.

2) Derive QThread

Derive QThread and add an additional signal started(bool) which will be emitted with a true parameter when started() signal is emitted and with a false parameter when finished() or terminated() signals are emitted although I must note that being notified by a started(false) signal that the thread has finished is not that intuitive especially if you're developing an API that will be used by other people.

If both approaches make you change a lot of code, then I would recommend using helper methods instead. Beside these, I don't think you have a better alternative.

Community
  • 1
  • 1
Iuliu
  • 4,001
  • 19
  • 31