Hi all, I have some questions on PyQt.
I created 2 sets of QDialogButtonBox, each with 3 buttons - ‘Add’, ‘Remove’, ‘Clear’.
Each set is connected/ for with a QListWidget, and since both buttons sets are working the same way except to different list, I created 3 separate methods, so that it is easier to maintain as I do not see the need to have a new method for each button.
Since I have used rejected
for the ‘Remove’ button, I used clicked
for the ‘Clear’ button since I am unable to see if there are any other signals that I can used for it.
However while it works for the ‘Add’ and ‘Remove’ buttons, it is not working ‘Clear’.
If either ‘Add’ or ‘Remove’ is clicked onto, the command for ‘Clear’ is called as well which ends up emptying the list everytime I tried to remove or add new item(s).
This is some portions of my code:
# List widget 01
self.widget_01_btns = QtGui.QDialogButtonBox()
self.widget_01_btns.addButton('Add', QtGui.QDialogButtonBox.AcceptRole)
self.widget_01_btns.addButton('Remove', QtGui.QDialogButtonBox.RejectRole)
self.widget_01_btns.addButton('Clear', QtGui.QDialogButtonBox.ResetRole)
# List widget 02
self.widget_02_btns = QtGui.QDialogButtonBox()
self.widget_02_btns.addButton('Add', QtGui.QDialogButtonBox.AcceptRole)
self.widget_02_btns.addButton('Remove', QtGui.QDialogButtonBox.RejectRole)
self.widget_02_btns.addButton('Clear', QtGui.QDialogButtonBox.ResetRole)
# Button Connections
# For widget 01
self.widget_01_btns.accepted.connect(lambda:self.add_objects(self.widget01_list))
self.widget_01_btns.rejected.connect(lambda:self.remove_objects(self.widget01_list))
# This is called in conjunction whenever either 'Add' or 'Remove' button is clicked
self.widget_01_btns.clicked.connect(lambda:self.clear_objects(self.widget01_list))
# For widget 02
self.widget_02_btns.accepted.connect(lambda:self.add_objects(self.widget02_list))
self.widget_02_btns.rejected.connect(lambda:self.remove_objects(self.widget02_list))
# This is called in conjunction whenever either 'Add' or 'Remove' button is clicked
self.widget_02_btns.clicked.connect(lambda:self.clear_objects(self.widget02_list))
I tried another method for the ‘Clear’ button that I found online, but upon using it, I got this error instead - AttributeError: 'NoneType' object has no attribute 'clicked'
self.widget_01_btns.button(QtGui.QDialogButtonBox.Reset).clicked.connect(lambda:self.clear_objects(self.widget01_list))
Any pointers for me?