dissidia
dissidia

Reputation: 1581

Same signals but to different buttons

I have 2 sets of 3 push buttons, each with a QDialogButtonBox() as follows:

Something like that:

self.set1_btns = QtGui.QDialogButtonBox()
self.set1_btns.addButton("Add", QtGui.QDialogButtonBox.AcceptRole)
self.set1_btns.addButton("Remove", QtGui.QDialogButtonBox.RejectRole)
self.set1_btns.addButton("Clear", QtGui.QDialogButtonBox.ResetRole)

where their role is about the same, the only difference between them is that they will be adding/removing/clearing each of their own QListWidget that I have 'assigned' to them.

To simplify things, the sets are as follows: (ListX - QListWidget, Add/Remove/Clear - QPushButton)

This is my code:

def connect_signals(self):
    # List1 functions - Add, Remove, Clear
    self.set1_btns.accepted.connect(self.add_objects)
    self.set1_btns.rejected.connect(self.remove_objects)
    self.set1_btns.clicked.connect(self.clear_objects)

    # List2 functions - Add, Remove, Clear
    self.set2_btns.accepted.connect(self.add_objects)
    self.set2_btns.rejected.connect(self.remove_objects)
    self.set2_btns.clicked.connect(self.clear_objects)

def add_objects(self):
    selections = ['aaa', 'bbb', 'ccc']
    for sel in selections:
        # I can only define it to add to list1
        self.list1.addItem(sel)

def remove_objects(self):
    for item in self.list1.selectedItems():
        self.list1.takeItem(self.list1.row(item))

def clear_objects(self):
    # self.list1_btns are the QDialogButtons
    role = self.list1_btns.buttonRole(button)
    if role == QtGui.QDialogButtonBox.ResetRole:
        self.list1.clear()

My question here would be how can I tell my function to tell which button from which of the sets are being click unto?

For sanity sake, I thought it will be ideal to combine into 1 functions since they work about the same, instead of writing 2 separate functions where the only changes I made will be the QListWidget variable.

Upvotes: 0

Views: 344

Answers (1)

qurban
qurban

Reputation: 3945

You can pass the QListWidget objects as arguments to those methods as follows:

def connect_signals(self):
    # List1 functions - Add, Remove, Clear
    self.add1.accepted.connect(lambda: self.add_objects(self.list1))
    self.remove1.rejected.connect(lambda: self.remove_objects(self.list1))
    self.clear1.clicked.connect(lambda: self.clear_objects(self.list1))

    # List2 functions - Add, Remove, Clear
    self.add2.accepted.connect(lambda: self.add_objects(self.list2))
    self.remove2.rejected.connect(lambda: self.remove_objects(self.list2))
    self.clear2.clicked.connect(lambda: self.clear_objects(self.list2))

def add_objects(self, listWidget):
    selections = ['aaa', 'bbb', 'ccc']
    for sel in selections:
        # I can only define it to add to list1
        listWidget.addItem(sel)

def remove_objects(self, listWidget):
    for item in listWidget.selectedItems():
        listWidget.takeItem(listWidget.row(item))

def clear_objects(self, listWidget):
    # self.list1_btns are the QDialogButtons
    #role = self.list1_btns.buttonRole(button)
    #if role == QtGui.QDialogButtonBox.ResetRole:
    listWidget.clear()

Where lambda is an anonymous function, provided by Python; the signal is connected to this anonymous function which contains a call to your method passing the list1 or list2 as arguments. Then the list objects are used inside your methods accordingly.

As you are connecting the clear buttons to only clear method, I believe that there is no need to check in clear_objects the role of the button. If you do want to check the role, you can pass the self.list1_btns and self.list2_btns as a second argument to clear_objects method.

Upvotes: 2

Related Questions