Nominate our 2022 Qt Champions!

How do a correct MVC Pattern in Qt?

  • Hello,
    I think I build a bad architecture in my application. I just implement the MVC pattern wrong I think. Here is my (probably wrong) solution:

    I have a view that have access to the model which is hold in my "controler".When the user do changes on view the view edit the data in model. For example when the user wants to save the data, he can click on a save
    button and the button calls a function saveData() from the contoler. The controler just use the model and call functions to update the data in database.

    I hope you understand the szenario and sorry for my bad english. What do you think about it? How can i do it better? Whats the disadvantage of my architecture?

  • Mhm can nobody help me? Or did I explain my problem too bad?

  • the idea of the model is to construct an access interface to datas.
    So... you need to put it inside a variable if you need to change/interact with some other event, OR reconstruct it each time.
    The way i do, for exemple for link two TreeView together (when i click on a line of the first treeview, the other one show me the linked datas...) is to memorize each model datas of my second treeview inside a QHash, and then, use "my_model_second_view->setModel(my_hash[id])" for show the correct model.

    Also, there what is a contrller is name in Qt: a delegate.

    there is some good explain aorund this on youtube (try to search at "deep dive model/view", it is a little bit long, and 6 parts, but really good explain for use it).

    hope this help.

  • There is a good article about it in the documentation (4.8).

    "see model-view-programming":

  • MVC is much broader than the item views implemented in Qt. And the items views implemented in Qt don't really follow MVC: there is no controller class involved, but a delegate is introduced.

    So, a general question about MVC architecture (which indeed, is too vague for me to get into) cannot be answered with a reference to how Qt implements item views.

  • But whats the reason why Qt didn't implement the common MVC pattern?
    Now I didn't use the model-view concept from Qt. Do you think the architecture I described in main post is okay? My goal was to capsule the view and logik.

Log in to reply