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. Tree selection clears when data model refresh
Forum Updated to NodeBB v4.3 + New Features

Tree selection clears when data model refresh

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 799 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.
  • A Offline
    A Offline
    AntriX
    wrote on last edited by
    #1

    I am having a treeview in which i have applied sorting/filtering using QSortFilterProxyModel. When i perform some action on tree item(ex. RMB action) I refresh my data model(QAbstractItemModel). I am using beginresetmodel() before refreshing my data model. After data model refreshed selection on selected item disappears. I have saved the selections before refreshing data model and applied them back after model refreshed but items not getting selected. I tried removing beginResetModel() before refreshing data model and slection works fine in that case.

    In QSortFilterProxyModel documentation it is written " However, beginResetModel() / endResetModel() returns the proxy model to its original state, losing selection information, and will cause the proxy model to be repopulated." Does anyone help me how can I retain selection while keeping beginResetModel() / endResetModel() when refreshing data model.

    Christian EhrlicherC 1 Reply Last reply
    0
    • A AntriX

      I am having a treeview in which i have applied sorting/filtering using QSortFilterProxyModel. When i perform some action on tree item(ex. RMB action) I refresh my data model(QAbstractItemModel). I am using beginresetmodel() before refreshing my data model. After data model refreshed selection on selected item disappears. I have saved the selections before refreshing data model and applied them back after model refreshed but items not getting selected. I tried removing beginResetModel() before refreshing data model and slection works fine in that case.

      In QSortFilterProxyModel documentation it is written " However, beginResetModel() / endResetModel() returns the proxy model to its original state, losing selection information, and will cause the proxy model to be repopulated." Does anyone help me how can I retain selection while keeping beginResetModel() / endResetModel() when refreshing data model.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @AntriX said in Tree selection clears when data model refresh:

      Does anyone help me how can I retain selection while keeping beginResetModel() / endResetModel() when refreshing data model.

      It can't since the model is - as the function already suggests - completely reset so the data may be completely different than before so how should selection retain?

      Use other methods to modify you model (e.g. begin/endInsert/RemoveRows)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @AntriX said in Tree selection clears when data model refresh:

        Does anyone help me how can I retain selection while keeping beginResetModel() / endResetModel() when refreshing data model.

        It can't since the model is - as the function already suggests - completely reset so the data may be completely different than before so how should selection retain?

        Use other methods to modify you model (e.g. begin/endInsert/RemoveRows)

        A Offline
        A Offline
        AntriX
        wrote on last edited by
        #3

        @Christian-Ehrlicher I have saved the selection before refreshing the model. can i use that selection somway and select the item back after refreshing?

        Christian EhrlicherC JonBJ 2 Replies Last reply
        0
        • A AntriX

          @Christian-Ehrlicher I have saved the selection before refreshing the model. can i use that selection somway and select the item back after refreshing?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AntriX said in Tree selection clears when data model refresh:

          I have saved the selection before refreshing the model. can i use that selection somway and select the item back after refreshing?

          How should this work - either the model changed that much that the previous selection can not longer be applied or you can modify your model so that you don't need to reset it...

          You can set a selection with the QSelectionModel

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • A AntriX

            @Christian-Ehrlicher I have saved the selection before refreshing the model. can i use that selection somway and select the item back after refreshing?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @AntriX said in Tree selection clears when data model refresh:

            I have saved the selection before refreshing the model. can i use that selection somway and select the item back after refreshing?

            I don't understand your expectation/what you intend to do to reselect.

            As @Christian-Ehrlicher has said if you only use insert/remove rows your selection should be preserved.

            If you reset the model you lose all selection. In order to restore what was selected: if you only saved the QModelndexes from the previous selection that is "useless", since it only saves row/column numbers and your new model may not have the same (e.g. perhaps the row count has changed or rows are in a different order). You cannot directly use a QModelIndex itself from the previous model to the reset model, though you can look at its row/column numbers.

            If you want to be sure to select the same, say, rows you will need to keep the primary keys of the rows selected so that you can use that to re-find the new rows with that PK. Or similar.

            Oh, I see, you have a tree model. You may have to save something like the complete "path" to selected items in order to relocate them.

            However, if you are sure the row numbers stay the same after the model repopulation you can then use the row numbers to reselect.

            When i perform some action on tree item(ex. RMB action) I refresh my data

            Do you mean you repopulate the model when the user clicks RMB? That would be frightening! Or do you mean the user invokes some action which changes the model and after that you reset model?? It is "a shame" to reset the model if you want to preserve selection, why does it need resetting?

            A 1 Reply Last reply
            3
            • JonBJ JonB

              @AntriX said in Tree selection clears when data model refresh:

              I have saved the selection before refreshing the model. can i use that selection somway and select the item back after refreshing?

              I don't understand your expectation/what you intend to do to reselect.

              As @Christian-Ehrlicher has said if you only use insert/remove rows your selection should be preserved.

              If you reset the model you lose all selection. In order to restore what was selected: if you only saved the QModelndexes from the previous selection that is "useless", since it only saves row/column numbers and your new model may not have the same (e.g. perhaps the row count has changed or rows are in a different order). You cannot directly use a QModelIndex itself from the previous model to the reset model, though you can look at its row/column numbers.

              If you want to be sure to select the same, say, rows you will need to keep the primary keys of the rows selected so that you can use that to re-find the new rows with that PK. Or similar.

              Oh, I see, you have a tree model. You may have to save something like the complete "path" to selected items in order to relocate them.

              However, if you are sure the row numbers stay the same after the model repopulation you can then use the row numbers to reselect.

              When i perform some action on tree item(ex. RMB action) I refresh my data

              Do you mean you repopulate the model when the user clicks RMB? That would be frightening! Or do you mean the user invokes some action which changes the model and after that you reset model?? It is "a shame" to reset the model if you want to preserve selection, why does it need resetting?

              A Offline
              A Offline
              AntriX
              wrote on last edited by
              #6

              @JonB said in Tree selection clears when data model refresh:

              Or do you mean the user invokes some action which changes the model and after that you reset model

              Yes user invokes some Action and i reset the model. That action can update the row or add new row just below the selected row.
              I am quite new to qt and dont know the way to update/add the row while not reseting the data model. Can you please point to some code/tutorial doing the similar operation?
              Also, After resting the model usually the row remains the same.

              Christian EhrlicherC JonBJ 2 Replies Last reply
              0
              • A AntriX

                @JonB said in Tree selection clears when data model refresh:

                Or do you mean the user invokes some action which changes the model and after that you reset model

                Yes user invokes some Action and i reset the model. That action can update the row or add new row just below the selected row.
                I am quite new to qt and dont know the way to update/add the row while not reseting the data model. Can you please point to some code/tutorial doing the similar operation?
                Also, After resting the model usually the row remains the same.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                @AntriX said in Tree selection clears when data model refresh:

                That action can update the row or add new row just below the selected row.

                Use other methods to modify you model (e.g. begin/endInsert/RemoveRows)

                Already told yopu how to do. And for the lazy ones: https://doc.qt.io/qt-6/qabstractitemmodel.html#beginInsertRows

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                3
                • A AntriX

                  @JonB said in Tree selection clears when data model refresh:

                  Or do you mean the user invokes some action which changes the model and after that you reset model

                  Yes user invokes some Action and i reset the model. That action can update the row or add new row just below the selected row.
                  I am quite new to qt and dont know the way to update/add the row while not reseting the data model. Can you please point to some code/tutorial doing the similar operation?
                  Also, After resting the model usually the row remains the same.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #8

                  @AntriX said in Tree selection clears when data model refresh:

                  That action can update the row or add new row just below the selected row.

                  No need to reset the model for such. To "add new row just below" use begin/endInsertRows() (in your implementation of insertRows(...)), as @Christian-Ehrlicher has said. To "update the row" just use setData() (your implementation must emit dataChanged(...)) to change columns in existing rows. Both of these will preserve existing selection correctly.

                  1 Reply Last reply
                  4
                  • A Offline
                    A Offline
                    AntriX
                    wrote on last edited by
                    #9

                    I am able to fix this by refreshing the seleced rows instead of reloading complete data model. Thanks both of you for the help.

                    1 Reply Last reply
                    0
                    • A AntriX has marked this topic as solved on

                    • Login

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