PyQt4 - 从 QListWidget 中删除项目小部件 [英] PyQt4 - Remove Item Widget from QListWidget
问题描述
我有一个 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.
我已经阅读了大量解决方案,但没有一个适用于我的特定场景.
目前,我只有实际的 Item Widgets 需要处理;不是它们的值或索引.
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.
这是因为我通过.selectedItems()
获取了项目(需要删除).
This is because I obtain the items (needed to be removed) via .selectedItems()
.
代码如下:
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.
如何删除它们?
我错过了什么吗?
我正在使用:
Python 2.7.1
PyQt4空闲 1.8
视窗 7
Python 2.7.1
PyQt4
IDLE 1.8
Windows 7
推荐答案
takeItem() 应该可以工作:
for SelectedItem in self.ListDialog.ContentList.selectedItems():
self.ListDialog.ContentList.takeItem(self.ListDialog.ContentList.row(SelectedItem))
这篇关于PyQt4 - 从 QListWidget 中删除项目小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!