PyQt4 - Remove Item Widget from QListWidget
Asked Answered
B

3

8

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

Brigidbrigida answered 20/9, 2011 at 11:55 Comment(0)
F
17

takeItem() should work:

for SelectedItem in self.ListDialog.ContentList.selectedItems():
    self.ListDialog.ContentList.takeItem(self.ListDialog.ContentList.row(SelectedItem))
Follmer answered 20/9, 2011 at 13:47 Comment(3)
Worked a charm! I'd seen the common solution was takeItem, but I was unsure on how to convert the item widget into an index. Thanks!Brigidbrigida
This is not quite clean. Taking an item will indeed remove it from the list, however the item is not actually 'deleted'. See docsFoolhardy
but these docs doesn't explain how to delete it!Rigmarole
P
8

Deleting an Item from ListWidget:

item = self.listWidget.takeItem(self.listWidget.currentRow())
item = None
Paregmenon answered 7/3, 2012 at 17:49 Comment(2)
Shouldn't garbage collector reclaim the memory if the returned "item" is not assigned to any variable? I think "takeItem()" should work without assigning its output to any variable and then setting that variable to "None".Thapsus
currentRow() only works for single-selection lists. Otherwise, one should use selectedItems() and iterate.Obscuration
I
2

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())
Ivied answered 20/9, 2011 at 12:48 Comment(3)
Actuall there is a method called takeItem (see other answers). That said, it is indeed weird that it is not called removeItem.Forthwith
@neuronet I assume they did not call it "removeItem" because the item is not destroyed, but actually returned with this function. i.e. item = listWidget.takeItem(row) will give you the object for the QListWidgetItem, which can then be passed to another widget or whatever.Foolhardy
@neuronet I heard the advice a while back that if there is an inconsistency in a well-designed language (like python, and Qt) it's usually for a good reason ;) #themoreyouknow!Foolhardy

© 2022 - 2024 — McMap. All rights reserved.