5

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.

RAM
  • 2,257
  • 2
  • 19
  • 41
r2p2
  • 384
  • 4
  • 14
  • My first comment was going to be to only update what changes in your model, but since you are using the SQL model, I doubt you can get that much visibility into changes. Is there some identifier you can get from the selected data to know which items are selected, and then find the model indexes when the model refreshes? – Caleb Huitt - cjhuitt Feb 14 '14 at 20:36
  • @CalebHuitt-cjhuitt I can get those identifiers but I don't know when the model refreshes so that I could reaply the selection. – r2p2 Feb 15 '14 at 22:18

1 Answers1

1

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 QModelIndexs and using them to again select the same cells.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
  • Sadly this is not working because the QIdentityProxyModel does not pass those signals. – r2p2 Feb 18 '14 at 18:13