QTableView retains selection after model update

I tried to create a user interface that displays the contents of the table while the data is updated every second.

Therefore, I have a chain of models:

  • QSqlTableModel - to access the contents of tables
  • MyModel - inherited from QIdentityProxyModel to slightly modify data (source is TableModel)
  • SomeFilterModels - which have MyModel as source

This chain ends with a QTableView. Since the QSqlTableModel is updated every second, any selection in the TableView is also deleted every second. Now I had two ideas to fix this.

  • Prevent detection of changes in TableModel. Which does not work well.
  • Capturing some events that were fired before and after the model is about to change for storage and restore the current selection. Unfortunately, QIdentityProxyModel does not transmit signals, for example, modelAboutToBeReset or modelReset or dataChanged .. it is also impossible to repeat these signals from MyModel because they are private.

I was looking for other ways to troubleshoot these update issues. But I can’t imagine that I am the first person to use a chain of proxy models in combination with periodically updating the model and choosing.

Can someone give me some advice?

Thanks in advance.

Perhaps worth noting:

  • One QSqlTableModel is used for many TableViews. (With a different FilterProxyModel chain.) Therefore, I cannot just stop updating, because one view has a choice.
  • , , . . , , TableView ProxyModels. .

, .

+4
1

QAbstractItemModel , , . , :

  • dataChanged
  • headerDataChanged
  • modelAboutToBeReset
  • modelReset
  • columnsAboutToBeInserted
  • columnsAboutToBeMoved
  • columnsAboutToBeRemoved
  • columnsInserted
  • columnsMoved
  • columnsRemoved
  • rowsAboutToBeInserted
  • rowsAboutToBeMoved
  • rowsAboutToBeRemoved
  • rowsInserted
  • rowsMoved
  • rowsRemoved

, , , , , Qt , , /, 't .

, modelAboutToBeReset , , , modelReset, , QModelIndex .

+1

Source: https://habr.com/ru/post/1525565/


All Articles