Search code examples
pyqtpyqt4pyqt5qbuttongroup

pyqt QbuttonGroup: Passing user data on buttonClicked


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


Solution

  • 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.