PyQt4 - Remove Item Widget from QListWidget

Anti Earth picture Anti Earth · Sep 20, 2011 · Viewed 24.8k times · Source

I have a QListWidget and I need to remove some items.

From what I've researched, this is a generally unpleasant thing to do.

I've read a tonne of solutions, but none are applicable to my specific scenario.
At the moment, I only have the actual Item Widgets to deal with; not their values or index.

This is because I obtain the items (needed to be removed) via .selectedItems().

Here's the code:

ItemSelect = list(self.ListDialog.ContentList.selectedItems())

for x in range (0, len(ItemSelect)):
    print self.ListDialog.ContentList.removeItemWidget(ItemSelect[x])

This does nothing at all, however.
It does not raise an error, but the selected items are not removed.
The methods I've seen for removing items require either the index or the name of the item, neither of which I have. I only have the actual widgets.

How do I remove them?

Am I missing something?

I'm using:

Python 2.7.1
PyQt4 IDLE 1.8
Windows 7

Answer

Avaris picture Avaris · Sep 20, 2011

takeItem() should work:

for SelectedItem in self.ListDialog.ContentList.selectedItems():
    self.ListDialog.ContentList.takeItem(self.ListDialog.ContentList.row(SelectedItem))