+ Reply to Thread
Results 1 to 2 of 2

Thread: QListWidget multi selection

  1. #1
    Join Date
    May 2007
    Location
    Australia
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QListWidget multi selection

    I have a dialog window which comes up with a list of items in a QListWidget * listWidget.
    The Dialog consists of the displayed list and OK/Cancel buttons.
    I set the selection to ExtendedSelection and now I can select multiple items from the list.
    When a selection is made (signal: itemSelectionChanged) I use:
    Qt Code:
    1. QItemSelectionModel * selections = listWidget->selectionModel();
    2. QModelIndexList indexes = selections->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 
    This code gives me the selected items that I then pass from the dialog (which will be closed) to another function which uses the info and saves the list as integer indexes.
    I use integer indexes since I have to use the location in the list for other purposes.

    My problems is:
    I want to select items in the listWidget when the dialog is opened.
    That way, if the user selected items once before, the next time he opens the dialog, the previous selection will be presented for him to change or leave as is.

    Should I save the QModelIndexList from before or is there a better way?
    e.g what is the right way to select items 2-5,6 in my list?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,506 Times in 1,384 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListWidget multi selection

    Quote Originally Posted by user View Post
    Should I save the QModelIndexList from before or is there a better way?
    e.g what is the right way to select items 2-5,6 in my list?
    QModelIndex is a temporary object. It shouldn't be stored because it might get invalid as soon as the model changes. I'd suggest identifying the items in another way.

    Qt Code:
    1. // set id
    2. int id = 123;
    3. item->setData(Qt::UserRole, id);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // get selected ids
    2. QList<int> ids;
    3. foreach (QListWidgetItem* item, listWidget->selectedItems())
    4. ids += item->data(Qt::UserRole).toInt();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // set selected ids (inefficient if there are lots of items)
    2. for (int i = 0; i < listWidget->count(); ++i)
    3. {
    4. QListWidgetItem* item = listWidget->item(i);
    5. int id = item->data(Qt::UserRole).toInt();
    6. item->setSelected(ids.contains(id));
    7. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    user (6th February 2008)

+ Reply to Thread

Similar Threads

  1. Replies: 18
    Last Post: 23rd July 2007, 05:58
  2. QListWidget selection behavior as in Windows XP
    By Levon Nikoghosyan in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 13:11
  3. Replies: 13
    Last Post: 15th December 2006, 11:52
  4. QTreeWidget & QListWidget different selection
    By munna in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2006, 06:50
  5. QListWidget selection behavior
    By Arthur in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2006, 14:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts