Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Utilizing MVC with Qt



  • Hello all,

    I have been looking into utilizing the MVC with Qt for a couple of days now and I have some questions to clarifiy a few details.

    So first, I read that Qt implements a MV design with delegates to communicate between the classes. I would like to utilize these data structures (QAbstractItemModel, QAbstractItemView, QAbstractItemDelegate) so that I don't need to create my own.

    In my application, I will have 3 or 4 views displaying various pieces of data (input text, check boxes, buttons, etc.). So far, I am thinking that each view will subclass the QMainWindow object and the QAbstractItemView class. This is so that I can utilize the Model and Delegate classes easily. However, from reading documentation, it would appear that the Model class is geared more towards data that can be displayed in a list.

    I do not have any list objects on my interface. Would I be able to store a mix of data in the Model class? I will be utilizing ints, boolean, and maybe strings.

    Also, would I be able to extract the information from the model class and change the display of buttons / text fields / check boxs / etc.? I am assuming that this is possible (Note: I only need to know if this is possible at this time. NOt so much focused on how to do it)


  • Moderators

    @Omni_Philm said in Utilizing MVC with Qt:

    Hello all,

    I have been looking into utilizing the MVC with Qt for a couple of days now and I have some questions to clarifiy a few details.

    So first, I read that Qt implements a MV design with delegates to communicate between the classes.

    Yes, usually the view is the controller. So strictly speaking it's kind of MV-VC.

    I would like to utilize these data structures (QAbstractItemModel, QAbstractItemView, QAbstractItemDelegate) so that I don't need to create my own.

    These classes are abstract. You need to derive them.

    So far, I am thinking that each view will subclass the QMainWindow object and the QAbstractItemView class.

    If you mean subclassing them simultaneously, then this simply ain't happening. None of those classes is virtually inherited. Aggregate instead.

    This is so that I can utilize the Model and Delegate classes easily. However, from reading documentation, it would appear that the Model class is geared more towards data that can be displayed in a list.

    The model is a data source, the view is data display. You can have classical MVC if you derive from QObject for example and name that class as controller, which holds and manages the view and the model.

    Would I be able to store a mix of data in the Model class?

    You can store pretty much everything in the model, although most of the time you'd consider an external data storage where the model is only interface to.

    Also, would I be able to extract the information from the model class and change the display of buttons / text fields / check boxs / etc.?

    Delegates.

    I am assuming that this is possible

    It is possible.



  • @kshegunov

    Thank you very much for your reply! It is quite helpful and clears up a few points. However, would you be able to further explain these points?

    I would like to utilize these data structures (QAbstractItemModel, QAbstractItemView, QAbstractItemDelegate) so that I don't need to create my own.

    These classes are abstract. You need to derive them.

    When I create a subclass, I thought that I was inheriting and derviving from the master class? Would you be able to explain what you mean a little bit more?

    So far, I am thinking that each view will subclass the QMainWindow object and the QAbstractItemView class.

    If you mean subclassing them simultaneously, then this simply ain't happening. None of those classes is virtually inherited. Aggregate instead.

    So basically I can't do something like this:

    class derivedClass : public QMainWindow, QAbstractItemView

    This is so that I can utilize the Model and Delegate classes easily. However, from reading documentation, it would appear that the Model class is geared more towards data that can be displayed in a list.

    The model is a data source, the view is data display. You can have classical MVC if you derive from QObject for example and name that class as controller, which holds and manages the view and the model.

    Great! That is good to know but if I can use the abstract classes, then it seems like the framework will take care of the nitty gritty details!



  • @Omni_Philm said in Utilizing MVC with Qt:
    We had talking about MVC here some time ago.

    What is very confusing with the Model-View-Delegate is that it is not MVC at all !
    In Qt, the View is de facto a Controller (usualy the top level object)
    and the Model is in fact a Data Source (as kshegunov pointing out)

    What is missing in Qt to become true MVC compliant ?
    The possibilty to create the controler object in the Designer (as Interface Builder on Mac) and doing all the signals and slots from it.
    That will be great isn't it ?

    It's a pity that the original creators of Qt don't go this way ...


  • Moderators

    @Omni_Philm said in Utilizing MVC with Qt:

    When I create a subclass, I thought that I was inheriting and derviving from the master class? Would you be able to explain what you mean a little bit more?

    Yes. But when the class is abstract there 2 things that are a bit more specific:

    1. You're obliged to provide implementations for the pure virtual functions. The abstract class is incomplete - it's the scaffolding in use, but the actual work (or part thereof) is up to you - in the derived class.
    2. As a consequence of 1 you can't have objects (instances) of abstract classes.

    So basically I can't do something like this

    Formally speaking you can, the compiler's going to do it, but it's wrong. So yes you can't in the sense of "you mustn't".


Log in to reply