PDA

View Full Version : Complete Model / View Seperation



DaveR
15th June 2015, 19:26
My colleagues are interested in refactoring a mid-size (15,000 line) application to use a complete separation of Qt from the model. That is to say, the model would not contain any "Q" types at all and would live in a separate directory. A controller would then be used to talk to the view.

We're used this architecture to good effect in the past, for a real-time application. In this case the threading model was more complex (since it was for real-time apps), so it was advantageous to have the underbelly in pure C++.

In the case of our current application however I'm not so sure. It's a single-threaded application and most of the data that would be contained in the "model" is view-related data anyway, such as panel positions and widget sizes.

My question is, how often is a complete separation of model from view an approach that is used in practice for Qt? Is this something you've personally used to good effect?

jefftee
15th June 2015, 19:28
Unless I'm misunderstanding your post, the Qt model/view paradigm already separates the data from the view. i.e. The model is responsible for the data and the view is how you wish to visually represent the data. Qt allows multiple views to operate on the same model and you can even show those multiple views separately at the same time.

d_stranz
15th June 2015, 22:04
My question is, how often is a complete separation of model from view an approach that is used in practice for Qt? Is this something you've personally used to good effect?

As jefftee says, Qt's model / view paradigm already implements a complete separation of models and views. If what you really mean is, "Is it possible to implement a model completely independently of Qt and use it in the Qt model / view architecture", the answer to that is yes, too.

I have a large pure C++ computational program that produces results as an instance of a plain vanilla C++ class structure. In the UI, I interface this to a model derived from QAbstractItemModel in order to display the results (through a set of proxy models) in a QTreeView, QTableView, and numerous graphical plots. The trick is that you need a way to tell the Qt model that the C++ model has changed. You don't have to expose the Qt paradigm to your C++ class, but simply implement a callback mechanism that lets the C++ model tell the Qt model something's changed. The Qt model can then issue signals to notify views to update their contents.

In my case, I store a pointer to the C++ class in my Qt model class, and override the data() method to retrieve the C++ model content and map it into Qt model items.