How to keep selection after sorting in QListView?



  • Hello!

    I have items displayed on QListView, I have a button to sort the list but after sorting the selections is cleared. how can I keep it?



  • Hi,

    I don't have much experience with Qt's Model/View classes but how about getting selectedIndexes() and select()'ing them via QItemSelectionModel which you can get with selectionModel()?



  • Yes but if select the item and get the index, after sorting this index will point to other item


  • Moderators

    [quote author="tokafr" date="1421937685"]
    I have items displayed on QListView, I have a button to sort the list but after sorting the selections is cleared. how can I keep it?
    [/quote]

    now the question is: how do you sort? Are you calling reset()?



  • after sorting I call listVIew->reset() to update the view yes



  • [quote author="tokafr" date="1421941756"]Yes but if select the item and get the index, after sorting this index will point to other item[/quote]

    QModelIndex is about the data in the model not about the view. Sorting would change the view not the data.


  • Moderators

    [quote author="tokafr" date="1421942474"]after sorting I call listVIew->reset() to update the view yes[/quote]
    this is your problem. reset() implicitly tells the view to throw away all the information it has (including selection) and to retrieve it again.

    You should use QSortFilterProxyModel for this case then. Which acts - as the name implies - as an proxy on top of your model and just rearranges the items for sorting.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.