PyQt: QListviewItemIterator

T

Tina I

Hi,
I'm fairly new to both Python and Qt so please bare with me.

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:


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?

Thanks
Tina
 
D

David Boddie

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
 
T

Tina I

David said:
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

Ah, of course!
Thanks!!

Tina
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top