Search code examples
c++qtsignals-slots

Is there a way to know which signal triggered slot in Qt?


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?


Solution

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