简体   繁体   中英

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM