71

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

Pamputt
  • 173
  • 1
  • 11
sorush-r
  • 10,490
  • 17
  • 89
  • 173

3 Answers3

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 ) 
   { 
      ...
   }

}
mitjap
  • 495
  • 1
  • 6
  • 14
Teh Suu
  • 1,759
  • 1
  • 11
  • 7
94

QObject::sender() will do the job.

Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44
Idan K
  • 20,443
  • 10
  • 63
  • 83
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