[PyQt] QTreeWidgetItemIterator

David Boddie david at boddie.org.uk
Mon Jul 30 23:33:27 BST 2007


On Mon Jul 30 22:33:55 BST 2007, Peter Shinners wrote:

> I'm not finding a clean way to iterate over all items inside a 
> QTreeWidget. It looks like QTreeWidgetItemIterator matches the C++ API, 
> but does not provide a Python iterator.
> 
> Am I going to need to call "value()" and operator "+= 1" to get through 
> this list?

It's not ideal, but you could create your own iterator class to help with
this task:

  class Iter(QTreeWidgetItemIterator):
      def __init__(self, *args):
          QTreeWidgetItemIterator.__init__(self, *args)
      def next(self):
          self.__iadd__(1)
          value = self.value()
          if value:
              return self.value()
          else:
              raise StopIteration

It looks like QTreeWidget could provide an __iter__() method to allow
an improved iterator to be used for constructs such as for loops. That
would also be useful.

David


More information about the PyQt mailing list