[英]pyqt4: less round-about way of removing item from QListWidget?
I want to remove an item whose name I know. 我想删除一个我知道名字的物品。 I came up with:
我想出了:
item = lw.findItems(name, QtCore.Qt.MatchExactly)[0]
lw.takeItem(lw.indexFromItem(item).row())
Is there any more direct way of doing this? 还有其他更直接的方法吗? Something closer to
lw.removeItem(name)
? 更接近
lw.removeItem(name)
吗?
This leaves a bit of ambiguity for multiple entries with the same text. 这给具有相同文本的多个条目留下了一些歧义。 I would lean more toward something like
我会更倾向于
[ lw.takeItem( i ) for i in range( lw.count ) if lw.item( i ).text() == name ]
This will remove all items matching name from the list. 这将从列表中删除所有与名称匹配的项目。 If you only want to remove the first instance, you need to expand this into a full for-loop that breaks at the first match.
如果只想删除第一个实例,则需要将其扩展为一个完整的for循环,该循环会在第一个匹配项时中断。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.