简体   繁体   中英

QTableView preserve selection after model refresh

I tried to build a user interface which displays the content of a table while the data is refreshed every second.

Therefore I have a chain of models:

  • QSqlTableModel - to access the tables content
  • MyModel - inherited from QIdentityProxyModel to modify the data a bit (source is the TableModel)
  • SomeFilterModels - which have MyModel as source

This chain ends in a QTableView . Because the QSqlTableModel is refreshed every second, any selection in the TableView is removed every second also. Now I had two Ideas to fix this.

  1. Prevent the TableModel from detecting changes. Which was not working very well.
  2. Catching some events fired before and after the model is about to change to store and restore the current selection. Sadly the QIdentityProxyModel does not forward signals like modelAboutToBeReset or modelReset or dataChanged .. it is also impossible to reemit those signals from MyModel because they are private.

I was searching for other ways to counter those refresh problems without success. But I can't imagine that I am the first person who uses a chain of proxy models combined with a periodic model refresh and selections.

Can anyone give me some tips?

Thanks in advance.

Maybe worth to note:

  • One QSqlTableModel is used for many TableViews. (With a different FilterProxyModel chain.) So I can't just stop refreshing because one View has a selection.
  • You may think that I know when I call the models refresh method. But for now it is a bit to complicated to pass this trough my ui architecture. I mean the model is updated and the TableView has already a connection to the updated model through some ProxyModels. There should be no need for another way of communication.

Hope my question makes sense.

QAbstractItemModel includes a number of signals that can help you know when the data in the model is or will be changing. In particular, it has the following signals:

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

Given that you lose the selection, I assume that the bolded signals are the ones you want to be concerned with, because the default Qt behavior is to preserve selection if they can where the columns or rows are removed/inserted and it doesn't affect the selection.

Once you are connected to these signals, in modelAboutToBeReset I suggest getting IDs for the cells that you can reuse to select them again, and in modelReset then using those IDs to get the QModelIndex s and using them to again select the same cells.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM