pyqt4:从 QListWidget 中删除项目的不太迂回的方法? [英] pyqt4: less round-about way of removing item from QListWidget?

查看:43
本文介绍了pyqt4:从 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())

有没有更直接的方法来做到这一点?更接近 lw.removeItem(name) 的东西?

Is there any more direct way of doing this? Something closer to 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 ]

这将从列表中删除所有匹配名称的项目.如果您只想删除第一个实例,则需要将其扩展为在第一次匹配时中断的完整 for 循环.

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.

祝你好运!

这篇关于pyqt4:从 QListWidget 中删除项目的不太迂回的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆