469,623 Members | 1,422 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

pyqt: multiple selections in QListView

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

--
Bob Parnes
rp*****@megalink.net
Jul 18 '05 #1
5 5671
Jim
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
Jul 18 '05 #2
On Tuesday 13 July 2004 1:53 am, 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.


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

Phil
Jul 18 '05 #3
On Tue, Jul 13, 2004 at 08:29:07AM +0100, Phil Thompson wrote:
On Tuesday 13 July 2004 1:53 am, 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.


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
--
Bob Parnes
rp*****@megalink.net
Jul 18 '05 #4
On Wednesday 14 July 2004 11:38 pm, Bob Parnes wrote:
On Tue, Jul 13, 2004 at 08:29:07AM +0100, Phil Thompson wrote:
On Tuesday 13 July 2004 1:53 am, 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.


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.


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
Jul 18 '05 #5
On Tue, 13 Jul 2004 00:25:02 -0700, Jim <ti*********@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
rp*****@megalink.net
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Vamsi Mudrageda | last post: by
reply views Thread by bill yeager | last post: by
2 posts views Thread by Fabian Steiner | last post: by
2 posts views Thread by Svenn Bjerkem | last post: by
2 posts views Thread by Tina I | last post: by
1 post views Thread by Reinaldo Carvalho | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.