pyqt4: less round-about way of removing item from

2019-09-16 07:35发布

问题:

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!