PyQt: QListView how to retrieve selected items?

F

Flavio

Hi,

I have a QListview widget that allows me to store a bunch of strings in
it. This strings can be visualized, sorted, selected, etc.

My Problem is that I cant find a way to get the user selected items
back from it! I looked over the Qt documentation many times over but
there is no method to that end.

Any PyQt guru lurking around?

should I try some other list widget, that has a "getSelected()"method?
Is there one?

any help will be appreciated.

Flavio
 
D

Diez B. Roggisch

Flavio said:
Hi,

I have a QListview widget that allows me to store a bunch of strings in
it. This strings can be visualized, sorted, selected, etc.

My Problem is that I cant find a way to get the user selected items
back from it! I looked over the Qt documentation many times over but
there is no method to that end.

Any PyQt guru lurking around?

should I try some other list widget, that has a "getSelected()"method?
Is there one?

QListViewItem.isSelected()? You have to loop over the items to get the
ones selected.

Additionally, you might want to register for the

selectionChanged

signal. In Single-selection-mode there even is a overloaded version that
directly gives you the selected item.

And all of this can be found within 20 seconds in the great Qt-Docs.
Especially easy with the included QAssistant, a help-browser with
indexing and whatever...

Diez
 
F

Fabian Steiner

Hi!

Sorry for adopting your post for my own question, but since it is
related to PyQT I think it's ok: Does anybody of you know where the
openbook »GUI Programming with Python: QT Edition« has gone? It's not
available any more: http://www.opendocs.org/pyqt/ points now to a
non-existing site.

So far, I also couldn't find any other site to have a look at the online
version.

Any hints are welcome!
 
F

Flavio

Iterating over the items and checking if it is selected, sounds like a
good idea, but there no obvious way to get a hold of the list of
items!! The only way you can get an item is if you are in single
selection mode and you call selectedItem(). But I have to use multiple
selection mode, for which this method does not work (it says so in the
docs)

The signal selectionChanged() is emitted whenever the set of selected
items has changed but does not return any information regarding the
items comprising the set. Also useless.
And all of this can be found within 20 seconds in the great Qt-Docs.
Especially easy with the included QAssistant, a help-browser with
indexing and whatever...

The docs are really great, it's this particular widget that appear to
lacking some important functionality! This is very sad.
 
D

Diez B. Roggisch

Flavio said:
Iterating over the items and checking if it is selected, sounds like a
good idea, but there no obvious way to get a hold of the list of
items!! The only way you can get an item is if you are in single
selection mode and you call selectedItem(). But I have to use multiple
selection mode, for which this method does not work (it says so in the
docs)

Who has created these items? Obviously you, so you _can_ store the list
of selected items.

Or you use the equally well documented QListViewItemIterator to traverse
the list of items.


regards,

Diez
 
P

Phil Thompson

Hi!

Sorry for adopting your post for my own question, but since it is
related to PyQT I think it's ok: Does anybody of you know where the
openbook »GUI Programming with Python: QT Edition« has gone? It's not
available any more: http://www.opendocs.org/pyqt/ points now to a
non-existing site.

So far, I also couldn't find any other site to have a look at the online
version.

Any hints are welcome!

I've been told that this is temporary. The book will be back, but I don't know
when.

Phil
 
F

Flavio

Who has created these items? Obviously you, so you _can_ store the list
of selected items.

well yeah, but the Idea was to let the user select(through the widget)
a subset of the original list and then access that subset...
Or you use the equally well documented QListViewItemIterator to traverse
the list of items.

Alright! Now you've hit the jackpot! I didn't know that class! There is
no reference to it in the QListView docs! This can solve my problem
(combined with isSelected()), though not as elegantly or efficiently as
having direct access to the set of selected items which already exists
but is not accessible...

Thanks Diez!
 
D

Diez B. Roggisch

Flavio said:
well yeah, but the Idea was to let the user select(through the widget)
a subset of the original list and then access that subset...


Alright! Now you've hit the jackpot! I didn't know that class! There is
no reference to it in the QListView docs! This can solve my problem
(combined with isSelected()), though not as elegantly or efficiently as
having direct access to the set of selected items which already exists
but is not accessible...

You are wrong thinking that this less efficient - on the contrary. It
might be less convenient.

The problem is that updating the selected-item-list (which can be a
tree, mind you!) on every user's click will be O(n). Maybe O(log(n)),
but that's with tricks and I'd have to think about that.

Additionally, maintaining that list in case of addition, deletion and so
on will always call for rebuilds.

In the end it might be that your user selects and deselects an item 100
times. But only when she hits the "export this list"-button you're
actually interested in the always-up-to-date data. So, efficiency is not
the problem here.

Diez
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top