Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qt4

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

like image 212
sorush-r Avatar asked Oct 28 '10 20:10

sorush-r


People also ask

What is signal and slot in PYQT?

A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

Can we connect one signal with multiple slots?

You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)


2 Answers

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 )     {        ...    }  } 
like image 57
Teh Suu Avatar answered Sep 16 '22 11:09

Teh Suu


QObject::sender() will do the job.

like image 30
Idan K Avatar answered Sep 18 '22 11:09

Idan K