Вопрос

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

Это было полезно?

Решение

takeItem() should work:

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

Другие советы

Deleting an Item from ListWidget:

item = self.listWidget.takeItem(self.listWidget.currentRow())
item = None

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())
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top