[Solved] Model/View design question
-
wrote on 7 Sept 2015, 23:09 last edited by Joel Bodenmann 9 Aug 2015, 09:58
I have a generic question regarding model/view design: When I have a custom subclass of a view class and a custom subclass of a model class, is it okay to make the model become a member of the view and create the model instance inside the constructor of the view class?
My reasoning to do so is that I can simply use the custom view class as a custom widget which I can use in my application. This way I save creating a custom widget class which has both the custom view and the custom model classes as members.
Any tips and hints are appreciated.
-
I have a generic question regarding model/view design: When I have a custom subclass of a view class and a custom subclass of a model class, is it okay to make the model become a member of the view and create the model instance inside the constructor of the view class?
My reasoning to do so is that I can simply use the custom view class as a custom widget which I can use in my application. This way I save creating a custom widget class which has both the custom view and the custom model classes as members.
Any tips and hints are appreciated.
wrote on 7 Sept 2015, 23:19 last edited byHi,
you can do it but, of course, is less flexible than using external model.
-
wrote on 7 Sept 2015, 23:43 last edited by
@mcosta Thanks for your answer.
So the proper way is to create three individual custom classes for the view, the model and the assembled widget? -
There are 2 components actually - the model and the view. The view is a widget already so you can use it directly. There's just a matter of where to keep the model.
You can either contain instances of both the view and model in some other class like you said or subclass the view and have the model as a member for convenience - similar to what QTreeWidget does with QTreeView and QStandardItemModel.
-
There are 2 components actually - the model and the view. The view is a widget already so you can use it directly. There's just a matter of where to keep the model.
You can either contain instances of both the view and model in some other class like you said or subclass the view and have the model as a member for convenience - similar to what QTreeWidget does with QTreeView and QStandardItemModel.
wrote on 8 Sept 2015, 09:27 last edited by@Chris-Kawa said:
You can either contain instances of both the view and model in some other class like you said or subclass the view and have the model as a member for convenience - similar to what QTreeWidget does with QTreeView and QStandardItemModel.
Which one is the recommended way? What are the benefits of having a separate widget class?
-
There's no recommendation AFAIK. It depends on your needs and how well each solution fits with the rest of your design.
Having it separate has the benefit of modularity - you can mix an match them without touching the other. But if your classes are specific to your needs and modularity is not something you're gonna use or need (which is not uncommon) then a compact "umbrella" class could be easier to maintain.
All in all it's really a detail and doesn't matter that much. Either way will work and you'll see solutions using both approaches out there. -
There's no recommendation AFAIK. It depends on your needs and how well each solution fits with the rest of your design.
Having it separate has the benefit of modularity - you can mix an match them without touching the other. But if your classes are specific to your needs and modularity is not something you're gonna use or need (which is not uncommon) then a compact "umbrella" class could be easier to maintain.
All in all it's really a detail and doesn't matter that much. Either way will work and you'll see solutions using both approaches out there.wrote on 8 Sept 2015, 09:58 last edited bySounds reasonable. Thank you for your help!
7/7