PyQt4 - Remove Item Widget from QListWidget

断了今生、忘了曾经 提交于 2019-12-01 01:26:52

问题


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


回答1:


takeItem() should work:

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



回答2:


Deleting an Item from ListWidget:

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



回答3:


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())


来源:https://stackoverflow.com/questions/7484699/pyqt4-remove-item-widget-from-qlistwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!