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. QTableView selection is lost when the underlying model changes
Qt 6.11 is out! See what's new in the release blog

QTableView selection is lost when the underlying model changes

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 6.1k 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.
  • napajejenunedk0N Offline
    napajejenunedk0N Offline
    napajejenunedk0
    wrote on last edited by
    #1

    If a QTableView's model has its row count changed in any way - rows were removed/inserted, the model was reset, the selection, if any, is lost. What I've tried to do is to preserve the selection before the rows count is modified and then restore it after the rows count is updated.

    I've connected to:

    • rowsAboutToBeInserted
    • rowsAboutToBeRemoved
    • modelAboutToBeReset

    when saving the current selection, and to:

    • rowsInserted
    • rowsAboutToBeRemoved
    • modelAboutToBeReset

    ... when restoring the selection if the previously selected model indices still exist.

    The problem is that nothing gets selected when restoring the selection,

    Any suggestions?

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

      do you really save the selection as QModelIndexes or just the plain rows as integers?

      --- 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
      • napajejenunedk0N Offline
        napajejenunedk0N Offline
        napajejenunedk0
        wrote on last edited by
        #3

        Tried both with saving the QItemSelection and the QModelIndexList which represents the selected indices - doesn't work. I even called selectAll on the view directly after the rows count changed but that didn't help, too.

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

          i thought so...

          From the QModelIndex docs:
          [quote]
          Note: Model indexes should be used immediately and then discarded. You should not rely on indexes to remain valid after calling model functions that change the structure of the model or delete items. If you need to keep a model index over time use a QPersistentModelIndex.
          [/quote]

          But IMHO you shouldn't restore the selection when the model got reseted. So you should be fine using QPersistentModelIndexes.

          --- 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
          • napajejenunedk0N Offline
            napajejenunedk0N Offline
            napajejenunedk0
            wrote on last edited by
            #5

            Currently, the selection model I use is configured to support multiple selection of rows only.
            The QPersistentModelIndex are invalidated as well. So, now I tried saving the row indices as pure integers and recreating the model indices when restoring the selection.

            So, I selected several rows, obtained their indices, recreated the model indices when restoring the selection, and started selecting each row using:
            @
            selectionModel->select( modelIndex, QItemSelectionModel::Rows | QItemSelectionModel::SelectCurrent );
            @

            The effect was that only the last row in the list of previously selected rows got selected. The QItemSelectionModel's documentation states that the flag QItemSelectionModel::Current preserves the current selection, so that using QItemSelectionModel::select along with this flag will result in updating the current selection.

            In addition, keeping the previously selected row indices is not an option, since if rows are inserted above the selected row, the selected row index would not be correct when restoring the selection. The QPersistentModelIndex way was a good option but despite being persistent, it again becomes invalidated.

            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