It's possible to bind more than one signal to one slot (isn't?). So, is there a way to understand which widget sends the signal? I'm looking for something like sender
argument of events in .NET
Asked
Active
Viewed 9.1k times
71
3 Answers
144
Use QObject::sender()
in the slot, like in the following Example:
void MainWindow::someSetupFunction( void )
{
...
connect( _foobarButton, SIGNAL(clicked()), this, SLOT(buttonPressedSlot()) );
}
void MainWindow::buttonPressedSlot()
{
// e.g. check with member variable _foobarButton
QObject* obj = sender();
if( obj == _foobarButton )
{
...
}
// e.g. casting to the class you know its connected with
QPushButton* button = qobject_cast<QPushButton*>(sender());
if( button != NULL )
{
...
}
}
-
1isn't qobject_cast better than plain dynamic_cast? – elephant Sep 24 '15 at 07:07
-
1@elephant Yes, as desribed on http://doc.qt.io/qt-4.8/metaobjects.html . This should be improved in the answer. – Johannes Dec 30 '15 at 20:56
8
Yes, you can connect multiple signals to one slot. In this case you would use QSignalMapper
to differentiate the sources of the signals. This solution is limited to parameterless signals. You can see an example here.

Arnold Spence
- 21,942
- 7
- 74
- 67