Unable to remove empty ListWidgetItem
-
Hi,
I'm using pyside2, and trying to make a list of custom widgets with QListWidget.I created a custom widget with a delete button like this:
class CustomWidget: deleted = Signal() def __init__() ... delete_button.clicked.connect(self.onDeleteClicked) def onDeleteClicked(self): self.deleted.emit() self.deleteLater()
And in the main widget with the ListWidget, I tried to connect the deleted signal to also remove the ListWidgetItem:
class MainWidget: def addItem(self): .... list_item = QListWidgetItem(self.list_widget) item.list_item = list_item item.deleted.connect(self.removeItem(item)) def removeItem(self, item): self.list_widget.removeItemWidget(item.list_item) self.list_widget.takeItem(self.list_widget.row( item.list_item))
I verified that removeItem was invoked when the custom widget was deleted, but the now empty ListWidgetItem stays there on the UI. And when I try to add things to the ListWidget again, a SegFault will be thrown.
Any experts on how to correctly remove a ListWidgetItem including its widget?
Update: Another thing I noticed was that even without clicking the delete button, removeItem() will always be called once in addItem() when the deleted signal is being connected. I assume this might have caused some reference to get lost at that stage, but why was it triggered when I'm just trying to connect the signal? (E.g. button.clicked.connect(onClicked) won't trigger onClicked unless actually clicked).
-
item.deleted.connect(self.removeItem(item))
This is not the right way to connect a slot to pass it a parameter, and does not do what you think. You need:
item.deleted.connect(lambda: self.removeItem(item))
-