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 contentQIdentityProxyModel
to modify the data a bit (source is the TableModel)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.
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:
QSqlTableModel
is used for many TableViews. (With a different FilterProxyModel chain.) So I can't just stop refreshing because one View has a selection.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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With