2

Assume that i have two signals (void signal1() and void signal2()) and one slot (void slot()). Both signals are connected to the slot:

connect(this, &Classname::signal1, this, &Classname::slot);
connect(this, &Classname::signal2, this, &Classname::slot);

In implementation of the slot(), is there a way to know which signal triggered this slot?

George
  • 578
  • 4
  • 21
  • 4
    Maybe `QObject::senderSignalIndex()` can help? – vahancho Aug 19 '20 at 12:43
  • If practical, you can use a DirectConnection and then just look at the call stack. – OMGtechy Aug 19 '20 at 12:54
  • 1
    Possible duplicate [how to get sender widget with a signal slot mechanism](https://stackoverflow.com/questions/4046839/how-to-get-sender-widget-with-a-signal-slot-mechanism)? – Andrej Aug 19 '20 at 13:51

1 Answers1

4

You could go through lambdas that provide the extra info...

connect(this, &Classname::signal1, this,
        [source = 1, this]()
        {
            slot(source);
        });
connect(this, &Classname::signal2, this,
        [source = 2, this]()
        {
            slot(source);
        });

(The above assumes your current implementation of Classname::slot takes no parameters -- adjust to suit.)

G.M.
  • 12,232
  • 2
  • 15
  • 18