pyqt: multiple selections in QListView
Bob Parnes
rparnes at megalink.net
Fri Jul 16 08:33:30 EDT 2004
On Tue, 13 Jul 2004 00:25:02 -0700, Jim <tiredofspam at anyaccount.com> wrote:
> Bob Parnes wrote:
>
>> I cannot find a reference on identifying multiple selections in
>> QListView. Apparently one has to iterate through all the items and test
>> each individually, but I don't know how to do the iteration. The qt3
>> documentation suggests QListViewItemIterator, but this does not seem to
>> be available in python.
>
> I think this will walk through all of the QListViewItems in a QListView
> (haven't tried it though) and give you a list of selected items:
>
> def getSelectedItems (listView):
> selectedLVIList = []
> listViewItem = listView.firstChild()
>
> scan(listViewItem, selectedLVIList)
>
> return selectedLVIList
>
> def scan(listViewItem, selectedLVIList):
> while listViewItem:
> if listViewItem.isSelected():
> selectedLVIList.append(listViewItem)
>
> scan(listViewItem.firstChild())
>
> listViewItem = listViewItem.nextSibling()
>
> The docs say that you might not traverse the list view items in sort order
> using firstChild/nextSibling. This also doesn't keep track explicitly of
> which level an item is on, although you can trace back each item's
> parent(s) (or else modify the code to track that).
>
> Jim
>
Thanks very much, the only error is that the nested call to scan needs
selectedLVList as a second argument. My list has only two levels, and I
do not need to maintain sort order, so this does what I need.
Bob
--
Bob Parnes
rparnes at megalink.net
More information about the Python-list
mailing list