Keep track of changes between QTableViews shared data
-
Hi,
I have recently taken over an QT project from a co-worker and have a question.
He used different QTableViews to display data and populate other views with the same data.Example: one main list of entries, make a selection of some entries and put the selected data in a new QTableView (sublist).
But when making changes in the main list, those changes are hard to keep track of in the sublists. Also it's very cumbersome to check if a selected row already exists (have to check for each row, each column if that value already exists or not) in the sublist before adding.
My solutions:
- Have a dynamic databound object "Entry" from which the data is shared between the QTableViews. Is this even possible?
- Give each row a hidden field with a generated ID, and keep track of the changes made for each row with that ID.
What would be the best way to solve this, without polluting the code too much?
I have coding experience ( i.a. C# and Java ) but no C++ yet.Thanks in advance!
-
QTableView only displays data. Same data model can be used for many views. So I think you don't need to add any more intermediate Entry or IDs - just use different filtering for each view.
An in-depth description of this whole system in Qt is here.
What comes to my mind first when I read your description of the problem is to suggest using QSortFilterProxyModel - it will allow you to display only selected list of items in the other views (while main view remains on "normal" model).
-
Awesome! Thank you for pointing out the proxy model.
I've created a custom ProxyModel where I store selected row indexes so it filters on row index.
The source model is still the same data so changes are the same everywhere. Exactly what I want..If a row/entry will be added before the ones selected in the proxy I'll have to notify the proxy and offset already stored indexes?
Or is there a better way of keeping track of the row position in the proxy itself? Like storing a row as an object?One thing I forgot to mention was that the data is from a CSV file, but I think that shouldn’t matter much for this case...
-
@SnakeMastr805 said in Keep track of changes between QTableViews shared data:
If a row/entry will be added before the ones selected in the proxy I'll have to notify the proxy and offset already stored indexes?
Proxy model will be informed about the change by your base model - as long as you don't forget calling beforeInsertRows(), endInsertRows() and such like (there is a whole family of similar functions in QAbstractItemModel class - for all item modification use cases).