How to determine who emitted the signal?

You might want to look into the QSignalMapper class, as it provides a means to associate either an int, string, or widget paramters to an object that sends a given signal. The main limitation is that the signal/slot being mapped needs to be parameter-less.

C++ example from the QT4.7 documentation:

 ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent)
     : QWidget(parent)
 {
     signalMapper = new QSignalMapper(this);

     QGridLayout *gridLayout = new QGridLayout;
     for (int i = 0; i < texts.size(); ++i) {
         QPushButton *button = new QPushButton(texts[i]);
         connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
         signalMapper->setMapping(button, texts[i]);
         gridLayout->addWidget(button, i / 3, i % 3);
     }

     connect(signalMapper, SIGNAL(mapped(const QString &)),
             this, SIGNAL(clicked(const QString &)));

     setLayout(gridLayout);
 }

You can find a PyQT4 example here, however when I have a chance I'll try to add a simple PyQT4 example.


I think that I opened a question too early, because I found an answer on google by myself. When slot is activated by emitter, the pointer of emitter stored, and can be retrieved by

QObject::sender()

and as a result can be accessed in PyQt by:

@QtCore.pyqtSlot()
def someSlot(self):
    self.sender()

Tags:

Python

Pyqt