0
QStandardItemModel::​QStandardItemModel(QObject * parent = 0)
Constructs a new item model with the given parent.

I thought models can share multiple views then why we are passing a widget to QStandardItemModel Constructor?

Freiza
  • 77
  • 1
  • 9
  • 1
    Note that parent is `QObject` so this can be owned by any object (main window for example). You can set this model to multiple item views and those views will not take over the ownership of model. – Marek R Feb 19 '15 at 08:42

1 Answers1

1

Actually QObject is not a widget, so that model is not dependent on any GUI component. The QObject argument passed to constructor because QStandardItemModel is a QObject itself and it follows Qt's standard parent-child relationship in QObjects hierarchy. If you want your model instance get deleted when its parent object destroyed, pass its pointer to the model's constructor.

vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Suppose I want to delete my model on deletion of a tableview should I pass tableview object to it? And If I pass no QObject can I simply write "delete qStandardModel" explicitly to delete the model? – Freiza Feb 19 '15 at 08:15
  • 2
    @Freiza, no, you shouldn't pass the view's pointer to the model, because the model can be bound to multiple views, so deleting one view will prevent other views from working. I think, the best approach is passing the pointer of all views' and their model's parent object . If no pointer is passed, yes, you can explicitly delete the model, but be careful - deleting model before deleting view can lead to crashes. – vahancho Feb 19 '15 at 08:35
  • How can I pass pointer to all views? Do you mean I pass the pointer of top-level widget because it contains all the views. And what is the model's parent object. Sorry for being such a noob. – Freiza Feb 19 '15 at 08:59
  • 1
    @Freiza, suppose you have a dialog. You pass dialog's pointer to all its buttons, views and models as dialog is the parent of all its children. When you destroy the dialog, its desctructor will delete all child objects automatically. For details read this: http://qt-project.org/doc/qt-4.8/objecttrees.html – vahancho Feb 19 '15 at 09:11