开发者

How to get sender widget with a signal/slot mechanism?

开发者 https://www.devze.com 2023-01-22 05:58 出处:网络
It\'s possible to bind more than one sig开发者_如何学运维nal 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 e

It's possible to bind more than one sig开发者_如何学运维nal 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


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

}


QObject::sender() will do the job.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号