Reputation: 15
I have defined my own class that inherits QbuttonGroup
class TupleButtonGroup(QtGui.QButtonGroup):
def __init__(self,parent, tuple_pair):
super(TupleButtonGroup, self).__init__(parent)
self.tuple_pair = tuple_pair
And on buttonclicked i want to access the user data tuple_pair and corresponding button clicked in the button group
button_group.buttonClicked[QtGui.QAbstractButton].connect(self.labelchanged)
def labelchanged(self, button):
print button.text()
The function receives the button clicked but i don't know how to access the user data in the callback
Upvotes: 1
Views: 1949
Reputation: 120678
All buttons have a group() method that allows you to get a reference to whatever group (if any) that they have been added to. So your code can simply be:
button_group.buttonClicked.connect(self.labelchanged)
def labelchanged(self, button):
print button.text()
print button.group().tuple_pair
And note that you dont need to specifiy QAbstractButton
when connecting the signal, because that is the default overload.
Upvotes: 1
Reputation: 37549
You could do it a couple of ways.
You could catch the buttonClicked
signal in your subclass and emit your own signal that contains the tuple_pair
.
class TupleButtonGroup(QtGui.QButtonGroup):
buttonClickedWithData = QtCore.pyqtSignal(tuple, QtGui.QAbstractButton)
def __init__(self,parent, tuple_pair):
super(TupleButtonGroup, self).__init__(parent)
self.tuple_pair = tuple_pair
self.buttonClicked[QtGui.QAbstractButton].connect(self.on_buttonClicked)
@QtCore.pyqtSlot(QtGui.QAbstractButton)
def on_buttonClicked(self, button):
self.buttonClickedWithData.emit(self.tuple_pair, button)
Then just connect to that new signal
button_group.buttonClickedWithData.connect(self.labelchanged)
@QtCore.pyqtSlot(tuple, QtGui.QAbstractButton)
def labelchanged(self, tuple_pair, button):
print tuple_pair
print button.text()
The other option is to use your existing class, signals and slots, and use the .sender()
method to get a reference to the button group from within the slot method.
@QtCore.pyqtSlot(QtGui.QAbstractButton)
def labelchanged(self, button):
btngroup = self.sender()
print btngroup.tuple_pair
print button.text()
Upvotes: 0