pyqt4: less round-about way of removing item from QListWidget?

吃可爱长大的小学妹 提交于 2019-12-25 08:37:05

问题


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


回答1:


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.

Good luck!



来源:https://stackoverflow.com/questions/6074198/pyqt4-less-round-about-way-of-removing-item-from-qlistwidget

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