Reputation: 1799
I'm doing some refactoring. I'm implementing a Model-View-Controller pattern. The view is a Qt widget.
Originally, the Qt widget created a new instance of a QAbstractTableModel subclass on the heap. Lets call it FooTableModel.
e.g
Widget::Widget(QWidget* parent)
:
QWidget(parent)
m_model(new FooTableModel(this))
{
Should I create the new instance of FooTableModel in the MVC model instead?
By doing so, I could create a dependency on the view (assuming I still pass the widget's pointer to the FooTableModel constructor)
Alternatively, I could pass nothing to the FooTableModel constructor and manually delete the FooTableModel in my MVC model. *
The last option would be to leave the creation of the FooTableModel in the widget. (And let the widget handle the FooTableModel directly?)
Any suggestions, or preferences?
My guess is to go with * at the moment.
Upvotes: 1
Views: 1547
Reputation: 3698
Generally you want to avoid passing the view to the model.
If your MVC model is a QObject and the FooTableModel instance is a child of it then you don't need to worry about the cleanup becasue Qt will do it for you. Ideally, if you are using Qt the FooTableModel would be THE model, or whatever had the instance of it would be.
Qt follows the Model/View pattern since the controller work is handled by the view. Check out: http://doc.trolltech.com/4.5/model-view-introduction.html for more.
Short answer: Pass nothing to FooTableModel, delete it when done.
Upvotes: 2