How can I remove a QListWidgetItem from a QListWidget and refresh the display?
Question
I am using a QListWidget to display a list of QListWidgetItem
This list is read from a file. When I close the file, I want to empty the list.
I did this method on my :
class QuestionsList(QtGui.QListWidget):
def __init__(self, parent):
super(QuestionsList, self).__init__(parent)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.setProperty("showDropIndicator", False)
self.setAlternatingRowColors(True)
self.quiz = None
def loadQuiz(self, quiz):
self.quiz = quiz
self.flush()
if quiz is not None:
i = 1
for question in quiz.questions_list:
self.addItem(QuestionItem(i, question, self))
i += 1
def flush(self):
for item in [self.item(i) for i in xrange(self.count())]:
print unicode(item.text())
self.removeItemWidget(item)
del item
The loadQuiz method works, the flush method print the text of each item but nor removeItemWidget method nor del item works to empty the list.
How can I do that ?
Thanks
Solution
Why wont use clear method on QListWidget ?
OTHER TIPS
I know this is already answered, but I came across this question to find a way to delete the selected item(s).
This can be done like so:
def removeSelected(self):
for item in self.selectedItems():
self.takeItem(self.row(item))
Hope this helps someone out there!
Actually, removeItemWidget doesn't work for this purpose.
Here is my solution
def flush(self):
while self.count() > 0:
self.takeItem(0)
The method takeItem(0) works like a pop() in a Stack and takeItem(count()-1) like a pop() in a queue.