pyqt: multiple selections in QListView

B

Bob Parnes

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.

Thanks for any help.

Bob Parnes
 
J

Jim

Bob said:
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
 
P

Phil Thompson

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.

QListViewIterator was added to PyQt v3.9 (current version is v3.12).

Phil
 
B

Bob Parnes

QListViewIterator was added to PyQt v3.9 (current version is v3.12).

Phil

Thanks very much. I am using v3.8 on a debian computer. I'll try to
upgrade.

Bob
 
P

Phil Thompson

Thanks very much. I am using v3.8 on a debian computer. I'll try to
upgrade.

At the moment it could be more Pythonic. You have to do something like...

it = QListItemViewIterator(list_view)

itm = it.current()

while itm:
# Do something

it += 1
itm = it.current()

In a future version I will make QListItemViewIterator behave like a Python
iterator so that you do this instead...

for itm in QListItemViewIterator(list_view):
# Do something

Phil
 
B

Bob Parnes

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

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top