First of all I would like to say that I' ve already read all the other questions about Qt and MVC, but I couldn't find what I'm looking for. So please, unless you find something in the old questions that actually answer my question, don't link them to me. I also searched in qt.digia.com and qt.project.com but again, no luck.
So now to my problem. I have to implement a simple image comparator that shows to image side by side so that they can be compared. I have to use MVC to do this. My problem is that I've never used Qt and I'm a bit confused on how to use it with MVC.
In particular, I am wondering what MainWindow should be subclassed from. It is the View or the Model, or both? This is what I thought. MainWindow is a View in my class diagram, but I'm not sure of that, because it also has elements of a model, since it actually stores data information. What do you suggest? Then how would design the other classes? Thank you.
MainWindow should sit between your view and your data model as a view controller, I would move the data information storage out into a data model and interact with it that way.
Qt does not implement the "standard" MVC pattern as a whole - you would need to reimplement such a framework from scratch. Qt offers a model-view framework that offers enough functionality for MVVM, but that's not MVC-by-the-book.
In Qt's implementation, the view and the controller are mingled together. The view is what shows the model to the user and what the user uses to interact with the model, with help of delegates.
Thus, the question of a separate controller is essentially moot, as there isn't one. In Qt, a concrete view is a stand-alone widget that you normally don't derive from. Instead, you integrate (has-a) the view into a larger widget that holds other controls.
Qt provides some standard views (a list view, a table view and a tree view). There's also the
QDataWidgetMapper
that lets you map one index from a model onto the user property of any widget. There are also several models to choose from. The abstract models are bases for your own implementations. Then there's theQStandardItemModel
that provides a flexible tree/table storage of data. Finally,QSqlQueryModel
andQSqlRelationalTableModel
expose SQL databases as models.In the example below, the comparator is implemented as a viewmodel - a proxy that amends the underlying image-providing model with results of the comparison. For the images to be displayed, they need to be provided under the
Qt::DecorationRole
. The main window is simply the view (is-a), and no subclassing is necessary.There are varying definitions of "MVC". If you mean Model-View-MEDIATING Controller, I think it is best to use the term MVA, i.e. Model-View-Adapter. Check this out: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93adapter
A class called MainWindow is absolutely part of a view per this model. The fact that the object is window makes it a type of view. I have a program for instance with three views: GuiView, TerminalView, and ServiceView. Those are radically different interfaces, which use the same underlying model, and have an adapter in between. Note: I also a virtual base class "View" which the adapter uses and thus doesn't care if I swap out derived view types.
There are plenty of ways to split up "views" other than the example I provided, but basically your view is how the user and/or client interacts with the program.
A window class should not be storing information, by any varied definition or interpretation of MVC.