Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt4 - Remove Item Widget from QListWidget

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

like image 667
Anti Earth Avatar asked Sep 20 '11 11:09

Anti Earth


3 Answers

takeItem() should work:

for SelectedItem in self.ListDialog.ContentList.selectedItems():
    self.ListDialog.ContentList.takeItem(self.ListDialog.ContentList.row(SelectedItem))
like image 190
Avaris Avatar answered Nov 09 '22 18:11

Avaris


Deleting an Item from ListWidget:

item = self.listWidget.takeItem(self.listWidget.currentRow())
item = None
like image 21
Cholavendhan Avatar answered Nov 09 '22 19:11

Cholavendhan


That's weird there isn't some direct way to delete items from QListWidget ... Try this:

listWidget = self.ListDialog.ContentList
model = listWidget.model()
for selectedItem in listWidget.selectedItems():
    qIndex = listWidget.indexFromItem(selectedItem)
    print 'removing : %s' %model.data(qIndex).toString()
    model.removeRow(qIndex.row())
like image 44
Jeannot Avatar answered Nov 09 '22 17:11

Jeannot