Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...