Mutual dependent model in Qt model view

How to use Qt’s Model-View framework to implement interdependent models? Specifically, how do you create a model that contains fields that refer to data in another model? I want the data changed/deleted in the first model to propagate to the subordinate fields in the second model.

For example, I have a model called BookListModel which contains a series of Books. I have a second model called ReaderTableModel, which contains a list of readers (names) and the books they are reading. I want these books to be able to reference the corresponding index of the BookListModel and propagate any changes to the corresponding in the ReaderTableModel Entry.

Does Qt have this mechanism? Can I store QPersistentModelIndex in another model?

It may be good to consider how relevant the data the model adapts to. If you allow the model to change the data source Update them, and update yourself when the data source changes, so you don’t have to worry about the interaction between BookListModel and ReaderTableModel.

The pattern is as follows: When BookListModel changes, it will update its containing books The data source of the data. Then, you will update the book data of the ReaderTableModel from the data source of each reader.

This mode follows Qt best practices and treats the models as data adapters instead of using them For data storage. http://qt-project.org/doc/note_revisions/13/174/view

How to use Qt’s Model-View framework to achieve interdependence Model? Specifically, how do you create a model that contains fields that refer to data in another model? I want the data changed/deleted in the first model to propagate to the subordinate fields in the second model.

For example, I have a model called BookListModel which contains a series of Books. I have a second model called ReaderTableModel, which contains a list of readers (names) and the books they are reading. I want these books to be able to reference the corresponding index of the BookListModel and propagate any changes to the corresponding in the ReaderTableModel Entry.

Does Qt have this mechanism? Can I store QPersistentModelIndex in another model?

It may be good to consider how the data the model adapts to are related. If the model is allowed to update them when the data source is changed, and update itself when the data source changes , You don’t have to worry about the interaction between BookListModel and ReaderTableModel.

The data source of the reader updates the book data of the ReaderTableModel.

This mode follows Qt best practices and treats models as data adapters instead of using them as data storage. http://qt-project. org/doc/note_revisions/13/174/view

Leave a Comment

Your email address will not be published.