Reputation: 1037
How do I implement interdependent models using Qt's Model-View framework? Specifically, how can I create a model that contains fields that reference data in another model? I want data that is changed/removed in the first model to propagate to the dependent field in the 2nd model.
Let's say for example I have a model called BookListModel
that contains a list of books. I have a second model called ReaderTableModel
that contains a list of readers (names) and the book that they are reading. I'd like these books to reference the corresponding index of BookListModel
, and any changes to propagate to the corresponding entry in ReaderTableModel
.
Does Qt have a mechanism for this? Can I store a QPersistentModelIndex
inside another model?
Upvotes: 4
Views: 548
Reputation: 799
It might be good to consider how the data your models adapt is related. If you allow the models to update their data sources as they are changed, and update themselves as the data sources change, you won't have to worry about the interaction between your BookListModel and ReaderTableModel.
The pattern would look like this: When a BookListModel changes, it will update its data source containing book data. Then you'll update your ReaderTableModel's book data from that data source for each reader.
This pattern follows a Qt best practice for treating models as data adapters and not using them as data stores. http://qt-project.org/doc/note_revisions/13/174/view
Upvotes: 3