[Qt-interest] How to find out which SIGNAL was sent
Andre Somers
andre at familiesomers.nl
Thu Nov 18 09:56:57 CET 2010
Op Do, 18 november, 2010 9:41 am, schreef NoRulez:
> Hello,
>
>
>
> how can I find out which signal was sent from a specific widget/QObject?
>
> For example:
>
>
>
> connect (myWidget1, SIGNAL(sig1()), this, SLOT(doSomething()));
>
> connect (myWidget1, SIGNAL(sig2()), this, SLOT(doSomething()));
>
> connect (myWidget2, SIGNAL(sig1()), this, SLOT(doSomething()));
>
> connect (myWidget2, SIGNAL(sig2()), this, SLOT(doSomething()));
>
>
>
>
>
> void MyClass::doSomething() {
>
> qDebug() << sender(); // myWidget1, MyWidget2
>
> }
>
>
>
> To find out the widget is simple, just call sender(), but how can I get
> the
> signal which was sent (in this example sig1() or sig2())?
You can't. Just like you can't with normal function calls.
If you need doSomething to act differently when called from different
signals, you should either use different slots, or use an argument that
carries the information you need. QSignalMapper can help you with
distinguishing signals from different senders (just like sender() can,
only a bit more structured), but for different signals you have to use an
argument you pass or use different slots.
André
More information about the Qt-interest-old
mailing list