4

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?

Terrabits
  • 997
  • 2
  • 15
  • 19
  • **Yes**, you can store `QPersistentModelIndex` in another model. Althogh it will become invalid when a book accosiated with it is removed. I would like to have an answer **is it a good approach or not?** I would never do this prefering to have pointers or ids instead of indices, but I can't prove it with a theory. – Ezee Aug 22 '14 at 07:10
  • I actually wanted the index becomes invalid situation in my use case. If the book were deleted, the reader would be left with nothing to read. In this case, I would denote a reader with no book by an invalid index, in which case this works. In your opinion, is this bad practice? I appreciate the input! – Terrabits Aug 22 '14 at 20:03

1 Answers1

3

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

Andrew Dolby
  • 799
  • 1
  • 13
  • 25
  • This is an interesting approach. To make sure I understand how to implement this: I could create two classes: Book and Reader. Reader would contain a reference to a book. I could then create QLists of each. I would then create classes BookListModel and ReaderTableModel that interact with these classes, and update as the underlying data stores (QLists, in this example) change. In this scenario, what mechanism would I use to notify a model that its underlying data store has changed? Is there a best practice recommendation for this interaction? – Terrabits Aug 22 '14 at 20:10
  • 1
    I would suggest that you create a signal/slot notification between your underlying data classes and the models. This way, you maintain the principle of keeping your classes as decoupled as possible. Your data store will signal that something's changed, your model will determine the indices and then emit the `dataChanged()` signal. The complexity of your underlying data store will determine whether the model is an example of the [Adapter Pattern](http://en.wikipedia.org/wiki/Adapter_pattern), or is as an example of the [Façade Pattern](http://en.wikipedia.org/wiki/Facade_pattern). – RobbieE Aug 24 '14 at 07:08
  • Thank you for the link. @terrabits Here is the theory I meant. I always prefer to store a data separately from models. Although it's more complicated. I call this View-Model-Storage approach. – Ezee Aug 24 '14 at 08:50