PyQt: QListviewItemIterator

David Boddie david at boddie.org.uk
Wed Jan 17 00:41:27 CET 2007


On Tuesday 16 January 2007 18:57, Tina I wrote:

> I have a QListView with a number of columns. In order to filter the
> output I iterate using QListViewItemIterator looking for the string
> entered by the user (filterString). Currently I do it this way:

[Reformatted code for quoting purposes]

> it = QListViewItemIterator(self.authListView)
> try:
>     while it:
>        item = it.current()
>        if item.text(0).contains(filterString) or \
>           item.text(1).contains(filterString) or \
>           item.text(2).contains(filterString):
>
>            item.setVisible(1)
>        else:
>            item.setVisible(0)
>        it +=1
> except AttributeError:
>     pass
> 
> Basically I iterate through the ListView until it goes beyond the list
> and raise an exception, which I catch. It works but to be honest it
> looks and feels ugly; "Do something until it goes wrong"
> 
> So, question: How can I know I have reached the last item in the
> QListView?

When it.current() returns None. You can rewrite what you already
have like this:

 it = QListViewItemIterator(self.authListView)
 while it.current():
     item = it.current()
     if item.text(0).contains(filterString) or \
        item.text(1).contains(filterString) or \
        item.text(2).contains(filterString):

         item.setVisible(1)
     else:
         item.setVisible(0)
     it += 1

If you don't like calling item.current() twice for some reason,
you could write this:

 it = QListViewItemIterator(self.authListView)
 item = it.current()
 while item:
     if item.text(0).contains(filterString) or \
        item.text(1).contains(filterString) or \
        item.text(2).contains(filterString):

         item.setVisible(1)
     else:
         item.setVisible(0)
     it += 1
     item = it.current()

David



More information about the Python-list mailing list