PyQt: QListView how to retrieve selected items?

Discussion in 'Python' started by Flavio, Feb 2, 2006.

  1. Flavio

    Flavio Guest


    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, Feb 2, 2006
    1. Advertisements

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

    Additionally, you might want to register for the


    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 B. Roggisch, Feb 2, 2006
    1. Advertisements

  3. 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: points now to a
    non-existing site.

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

    Any hints are welcome!
    Fabian Steiner, Feb 2, 2006
  4. Flavio

    Flavio Guest

    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

    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.
    The docs are really great, it's this particular widget that appear to
    lacking some important functionality! This is very sad.
    Flavio, Feb 2, 2006
  5. 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.


    Diez B. Roggisch, Feb 2, 2006
  6. I've been told that this is temporary. The book will be back, but I don't know

    Phil Thompson, Feb 2, 2006
  7. Flavio

    Flavio Guest

    Who has created these items? Obviously you, so you _can_ store the list
    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...

    Thanks Diez!
    Flavio, Feb 2, 2006
  8. 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 B. Roggisch, Feb 2, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.