Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to keep selection after sorting in QListView?

How to keep selection after sorting in QListView?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tokafr
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ckakman
      wrote on last edited by
      #2

      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()?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tokafr
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          [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()?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tokafr
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ckakman
              wrote on last edited by
              #6

              [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.

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                [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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved