Results 1 to 3 of 3

Thread: Reselect items in QListWidget from QList<QListWidgetItem *>

  1. #1
    Join Date
    Mar 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Reselect items in QListWidget from QList<QListWidgetItem *>

    Hi
    I've been searching around and am pretty sure this has not been covered.

    I am trying to select a list of items in a QListWidget. The list of items is currently in a QList<QListWidgetItem *> (the same as when you use QListWidget->selectedItems). So I basically want to step through the QList and select all items appearing in the QList in the QListWidget.
    What is the easies way to do this. I tried a couple of things, but with my best attempt, the program crashes in runtime .

    If it helps, the purpose for the code:
    The user selects a number of items in the QListWidget. He can then move the selected items up or down in the list (simply change the order). This is done by clicking on a button. The button then emits a signla that changes the order of the underlying QList containing the actual items in the entire QListWidget.
    At the same time, the user's current selection is saved to reselect these (hopefully...) after the order is changed.
    After the order is changed, the QListWidget is updated by first clearing it and then adding the entire reordered list. At this point I want to select the previous selection for the user so that he can click the "Change Order Button" again, without having to reselect the item to move.

    What I currently have: (I hope the code displays correctly)

    Qt Code:
    1. if ( _selection.count() > 0 )
    2. {
    3. foreach(QListWidgetItem *item, _selection)
    4. {
    5. ui->lstIncludedFolder->setCurrentItem(item, QItemSelectionModel::Select);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    _selection is the QList<QListWidgetItem *> list of previously selected items.
    lstIncludedFolder is the QListWidget containing the entire list of items.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reselect items in QListWidget from QList<QListWidgetItem *>

    First, there is a difference between a "current" item and a "selected" item. You want the latter, not the former. Then instead of fighting with QListWidget, you should use QListView with your own model that operates on the underlying list you already have. Then you wouldn't need to do anything as the model-view framework would do everything for you (at the cost of having to implement your own model).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reselect items in QListWidget from QList<QListWidgetItem *>

    Thanks Wysota. I'll give that a try

Similar Threads

  1. QListWidget and QListWidgetItem
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 3rd August 2011, 09:59
  2. Replies: 1
    Last Post: 22nd July 2010, 04:58
  3. using QStringList as QList<QListWidgetItem*>
    By geleven in forum Qt Programming
    Replies: 3
    Last Post: 8th April 2010, 18:13
  4. delete QListWidgetItem in QListWidget
    By sophister in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2009, 15:52
  5. QListWidget add QListWidgetItem
    By fellobo in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2006, 19:37

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
  •  
Qt is a trademark of The Qt Company.