0

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

  • "Add" (AcceptRole)
  • "Remove" (RejectRole)
  • "Clear" (ResetRole)

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)

  • Set1 : List1, Add1, Remove1, Clear1
  • Set2 : List2, Add2, Remove2, Clear2

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.

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
dissidia
  • 1,531
  • 3
  • 23
  • 53

1 Answers1

2

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.

qurban
  • 3,885
  • 24
  • 36
  • Please ignore my post.. It is actually working. Yes, initially I was connecting `clear` method to `clear` button – dissidia Feb 24 '17 at 01:46
  • Just a quick question though, is it possible for the `add` buttons of the 2 set to instinctively know each other? Eg. if I add 'itemA' in list1, when I press the add button for set2, list2 should not be adding 'itemA' because it is already in list1 – dissidia Feb 24 '17 at 01:52
  • I just realized that this code will wipe out any new additions if I tried to add any new objects. Eg. If list already contains 3 objects, and I tried to add another new object, the objects in the list got wipe out and now the list will only contains this 'new' single object only possibly due to `.clicked.connect` used – dissidia Mar 06 '17 at 22:53