[Solved] Model/View design question
-
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.
-
Hi,
you can do it but, of course, is less flexible than using external model.
-
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.
-
@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. -
Sounds reasonable. Thank you for your help!