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.