PyQt: QListView how to retrieve selected items?

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

  1. Flavio

    Flavio Guest

    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
    Flavio, Feb 2, 2006
    #1
    1. Advertising

  2. Flavio schrieb:
    > 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
    Diez B. Roggisch, Feb 2, 2006
    #2
    1. Advertising

  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: 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!
    Fabian Steiner, Feb 2, 2006
    #3
  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
    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.
    Flavio, Feb 2, 2006
    #4
  5. Flavio schrieb:
    > 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
    Diez B. Roggisch, Feb 2, 2006
    #5
  6. On Thursday 02 February 2006 9:25 pm, Fabian Steiner wrote:
    > 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
    Phil Thompson, Feb 2, 2006
    #6
  7. Flavio

    Flavio Guest

    > 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!
    Flavio, Feb 2, 2006
    #7
  8. Flavio schrieb:
    >> 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...


    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
    Diez B. Roggisch, Feb 2, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Uwe Mayer

    fast QListView numerical sort

    Uwe Mayer, Feb 12, 2004, in forum: Python
    Replies:
    2
    Views:
    487
    Uwe Mayer
    Feb 12, 2004
  2. Bob Parnes
    Replies:
    5
    Views:
    1,148
    Bob Parnes
    Jul 16, 2004
  3. Svenn Bjerkem

    Removing an item from a QListView in PyQt

    Svenn Bjerkem, Apr 11, 2006, in forum: Python
    Replies:
    2
    Views:
    845
    David Boddie
    Apr 11, 2006
  4. Iain
    Replies:
    3
    Views:
    928
  5. divya
    Replies:
    1
    Views:
    1,086
    Munna
    May 28, 2008
Loading...

Share This Page