I'd subclass QPushButton
and define my own sender and slot. The QObject.sender()
method is tempting, but it gives me the heebie-jeebies.
class MyPushButton(QPushButton):
def __init__(self, text = '', parent = None):
QPushButton.__init__(self, text, parent)
self.clicked.connect(self._handle_click)
my_click = QtCore.pyqtSignal(QObject)
def _handle_click(self):
my_click.emit(self)
def btnclick(btn):
print 'Handle button %s' % btn
for i in xrange(20):
btn = MyPushButton('%s %s' % ('Button', i + 1), self)
btn.my_click.connect(btnclick)
A slightly more Pythonic way of doing this could define the behavior within the class, like so:
class MyPushButton(QPushButton):
def __init__(self, button_number, parent = None):
QPushButton.__init__(self, '%s %s' % ('Button', button_number), parent)
self.button_number = button_number
self.clicked.connect(self._handle_click)
def _handle_click(self):
print 'Handle button %s' % self
for i in xrange(20):
btn = MyPushButton(i + 1, self)