pyqt: multiple selections in QListView

Jim tiredofspam at anyaccount.com
Tue Jul 13 03:25:02 EDT 2004


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





More information about the Python-list mailing list