Searching a PyQT List View?

Jim jbublitzNO at SPAMnwinternet.com
Fri Jan 3 03:02:29 EST 2003


Wayne Pierce wrote:
> I have an array of arrays, containing information I would like to put
> in to a List View.  The problem I seem to have, is that I cannot
> figure out how to determine if part of the data already exists in the
> list view.

> Each line of the array is in the following format:

>  [src,dst,proto,port,alert]

> All of the items are strings, except for 'port'.  I can add the items
> using:

>   QListViewItem(self.lstvwRules, i.src, i.dst, i.proto, str(i.port),
> i.action)

That will give you a single entry with 5 columns.

> This adds each item fine, however it is all one line which cannot be
> collapsed; so I have to add each item as a child QListViewItem. 
> Something like this:
> 
>   src = QListViewItem(self.lstvwRules, i.src)
>   dst = QListViewItem(src, i.dst)
>   proto = QListViewItem(dst, i.proto)
>   port = QListViewItem(proto, i.port)
>   alert = QListViewItem(port, i.alert)

> (I have not actually tested the above, but it seems logical)

That will give you a tree view (like a directory tree), but
only a single column. You'd have to open each level to see
its value. Another alternative would be to put dst, proto,
port and alert all on the same level under src, or some other
combination that shows what you want when collapsed/expanded.
Depends on how you want it to look/operate.

> The question I have is how to find the value of a column after the
> QListViewItem has already been added.  For example, is there a way to
> get the first item of the first column, then if it matches the data I
> want to enter check the second item, etc.?

> Thanks for any pointers,

No pointers in Python, only instances.

You can iterate over all of the items by navigating the tree
with QListView.firstChild, itemBelow/itemAbove, and
QListViewItem.text(col), or you can search  for a single item with 
QListView.findItem. (see the Qt docs for QListView). What I've
done a few times is to construct/maintain a Python list or dict
that mirrors the QListView. If you have a small number of items
it doesn't take a lot more memory, and it's less work in general
if you want to merge lists, write the list to a file, etc.


Jim





More information about the Python-list mailing list