By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

PyQt: QListviewItemIterator

P: n/a
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
Jan 16 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jan 16 '07 #2

P: n/a
David Boddie wrote:
>
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
Jan 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.