Jump to content
  • Advertisement
Sign in to follow this  

mvc and c++

This topic is 2379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello i'm trying to implement an mvc pattern in c with qt.
The first two doubt different implementation are these:
1)the view reacts to a button pression
2)the view call a function related to this button on the controller
1)the controller calls some function on the model and the model return decoupled are informations.
2)the controller calls a function in the view and the view function take the controller informations(returned by the call or calls on the model functions) and visualize it

1a)the controller calls some function on the model and the model send itself to the view with an observer pattern
2a)the view take from the model information whats it want and visualizes it

My problem to choose one approach or the other is
1)that with the first i must create and populate some agnostic classes for trasport the information from the model to the view.

2)in this how i can do for take only a piece of data from the interface?
For example if i call a function A in the controller and A execute a function on the model, the model send itself to the view......
and now the view has access to the model interface, but how known what information it must take for his visualization?

Maybe I understand something wrong, in some example i see that there are more model and more views, but in this case , if i have more than a model is a restriction , because if some functions are in model A and some in model B, how i can create a function always in the model A that interact with another function in model B?
the functions are sparse in multiple models
can you help me to understand?


Share this post

Link to post
Share on other sites
The model view controller works for web site design because thats all there is. In c++ programming, there are many more ways to seperate code than the MVC route because you are not just developing a web application.

You can seperate code based on input, network, graphics, logic, threading... on and on

Segregation of code is done in c++ to make compilation faster, reduce coupling between classes, keep cpp files as small as possible to make maintining easier, and to create logical relationships between child and parent classes. There are other reasons I am sure, but I would suggest to not think of web programming paradigms as a way to program a c++ application. The two might be somewhat related, but in general they are not.

Share this post

Link to post
Share on other sites
Model-View-Controller predates the web by a decade. There are alternatives, but this is hardly a square peg in a round hole. OP, I think the observer pattern makes this easier: have the view callbacks go directly to the controller (or via a thin wrapper if this avoids dragging Qt into otherwise-unrelated code) and the view subscribe to relevant updates from the model.

Share this post

Link to post
Share on other sites
A MVC implenentation in an user application is very different from that one on a web server.
One of the major differences is that web frameworks are often stateless but user application are not. Also in web frameworks, the model is basically the same as the "database" which is not always the right approach for a user application.

We are developing a game engine in C++ and here is rough overview how it works.

Listens for input from devices, network and GUI events from the presentation, create events of these and sends to the logic.

Logic (state machine)
Acts upon events from the controller, in most cases events results in the logic changes state or add data to the model.

The model holds all persistant data, it is basically just a container.
In our engine, the model keeps a map of "game objects", where a game object is a just an ID with a set of properties, like {"mesh", "robot.mesh"} or {"mana", 124}.

When objects are created/destroyed or updates with properties, the view reacts, for example when the property {"mesh", "robot.mesh"} is set, the presentation loads that mesh for the object.

Share this post

Link to post
Share on other sites
You're using Qt. One of your views has a button with a 'click' signal you connect to a 'on_frobnicate_button_clicked' slot in your controller. The controller reacts accordingly, like by frobnicating fooby data from the model. When the data is ready, it emits its 'fooby_updated' signal. The views have slots connected to that signal and update their presentation.

Qt is finely tuned for doing MVC programming. How you divide between model, view, and controller is stuation-specific and really depends on your view, your data sources, and your manager. There is no one-size-fits-all approach, but proper use of Qt's signals and slots lets you decouple very nicely and avoid useless middle classes. Avoid the use of Java programming when writing C++.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!