I am writing a learning task, trying to write some semblance of MVC. I am trying to divide the logic of the program and the interface into different classes. Accordingly, I wrote down the Controller
and View
classes. View
contains pressing any buttons, reading from fields, updating the contents of tags, etc. Controller
, in theory, should initiate the launch of the View
and read the resulting values to conduct operations on them and transfer this good to the model. I made a view of the controller element, I called it, but I ran into a problem - how can I actually get data from the view? Those. for example, I understand that you can run view->show()
and other methods to call from the controller, but how to ensure free work with the form to the user and at the same time receive information about his actions?
|
1 answer
So you have views of the view and so work on the events of the user. And in these slots controller methods should be called. But just in the controller there should not be calls of the view methods, otherwise they turn out to be tied strongly. The controller events (according to its signals) must also be updated view.
- I correctly understood that you are offering me to take up the program "from the other end" - i.e. make the controller a view element, and the model an element of the controller, and call the controller methods according to the slots? Those. access to the model from the view should then be organized from the controller's special method in order not to break the MVC pattern, right? - ASG
- Not so little. You have view, model and controller. Events in the system occur when? When the user presses the buttons. So in the slots of the buttons and called the controller methods. The model you have is updated, read when? Sometimes by pressing the buttons - sometimes in the controller. So call the model methods somewhere in the slots, somewhere in the controller. Access to the model you have organized in the methods of the model. (And not in the controller). The controler is also not an element of the view (of course, one object of the controller can be entered as a member of the view class), but this object is for accessing the controller's methods, and processing data only in none - Mira
- Only in controller methods. And since one should strive not to get extra members of the class, the controller is still better to start locally in the necessary methods, the controller object started at the touch of a button, its data processing method was started (and in the processing method they called the save data model method) at the end of the button press slot, everyone died, having completed theirs. - Mira
- And if you organize direct access to the model from the view, isn’t the concept of the MVC template violated? Usually, there is no direct arrow on the diagrams from the view to the vr-online.ru/article/pattern/screen1.jpg model . And another question - I honestly did not understand if the controller is not made a member of the view class as a separate and independent object, then how then contact him then? - ASG
- Not a member of the class - but a local variable in the class method. Why get a member of a class if it does not display the state of the object? (And the controller does not store and does not display the state of the view). Regarding direct access from the view to the data - you will not have direct access to the data! A model is not data, it is a class that is responsible for their storage, recording, reading. In this class there is a method to "write" you call it from twist. And already in this method, hidden from twisting, the recording is done where necessary. In DB, file, registry or internal variables of the model. Thus, work with data is taken out separately. - Mira
|