Take the 2-minute tour ×

Currently, I use the following while loop in a class that inherits QtGui.QListWidget to iterate all items:

    i = 0
    while i < self.count():
        item = self.item(i)

        i += 1

I was hoping I could use:

for item in self.items():

but the items() method wants a QMimeData object which I don't know how to construct to return all items. Is there a cleaner approach than my while loop above?

share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

I don't know what's it with the MIME type either, and I couldn't find a convenience method either. But you could write a simple method like this and be done:

def iterAllItems(self):
    for i in range(self.count()):
        yield self.item(i)

It's even lazy (a generator).

share|improve this answer
Thanks! What are your thoughts on using xrange here. –  majgis Jan 7 '11 at 21:10
@majgis: My all means, yes. In Python 2, use xrange whenever possible. It's just that I'm usually using Python 3, so I get tend to write Python 3 in my examples. –  delnan Jan 7 '11 at 21:14
add comment

I know this is old but, I just found out a function findItems(text, Qt.MatchFlags) in QListWidget. So, to iterate all items:

#listWidget is a QListWidget full of items
all_items = listWidget.findItems('', QtCore.Qt.MatchRegExp)
for item in all_items:
  print item

And do whatever you need with the item =)

share|improve this answer
add comment
items = []
for index in xrange(self.listWidget.count()):
share|improve this answer
add comment

Your Answer


