Tree selection clears when data model refresh
-
wrote on 20 May 2023, 08:19 last edited by
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.
-
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.
@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)
-
@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)
wrote on 20 May 2023, 08:41 last edited by@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-Ehrlicher I have saved the selection before refreshing the model. can i use that selection somway and select the item back after refreshing?
@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
-
@Christian-Ehrlicher I have saved the selection before refreshing the model. can i use that selection somway and select the item back after refreshing?
wrote on 20 May 2023, 09:14 last edited by 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
QModelndex
es 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 aQModelIndex
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?
-
@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
QModelndex
es 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 aQModelIndex
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?
wrote on 20 May 2023, 09:33 last edited by@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. -
@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.Lifetime Qt Championwrote on 20 May 2023, 09:34 last edited by Christian Ehrlicher@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
-
@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.wrote on 20 May 2023, 09:41 last edited by JonB@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 ofinsertRows(...)
), as @Christian-Ehrlicher has said. To "update the row" just usesetData()
(your implementation mustemit dataChanged(...)
) to change columns in existing rows. Both of these will preserve existing selection correctly. -
wrote on 22 May 2023, 06:44 last edited by
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/9