Please RTM 
Look here : http://doc.qt.nokia.com/4.7/objecttrees.html and http://doc.qt.nokia.com/4.7/qt-basic-concepts.html
Basically any new object that is QObject subclass uses parent/child relationship. This means that if parent is deleted all children are deleted to.
In above example it you delete "this' then model and view will be deleted to.
If you write above code like this:
qDebug() << model->parent();
cb->setModel( model );
qDebug() << model->parent();
delete cb;
QComboBox *cb = new QComboBox( this );
QStandardItemModel *model = new QStandardItemModel( cb );
qDebug() << model->parent();
cb->setModel( model );
qDebug() << model->parent();
delete cb;
To copy to clipboard, switch view to plain text mode
deleting cb will delete model also.
Althought if You delete model from view then view don't "know" what/how to display. By default views i.e. QComboBox creates QStandardItemModel in constructor so after You delete Your custom model You will have to add new one, something along this line's:
model->setObjectName( "My Old model" );
qDebug() << model->parent();
cb->setModel( model );
qDebug() << model->parent();
delete model;
cb->model()->setObjectName( "New model" );
cb->addItem( "sample", "Sample" );
qDebug() << "Model name:" << cb->model()->objectName() << "Parent: " << cb->model()->parent();
QComboBox *cb = new QComboBox( this );
QStandardItemModel *model = new QStandardItemModel( cb );
model->setObjectName( "My Old model" );
qDebug() << model->parent();
cb->setModel( model );
qDebug() << model->parent();
delete model;
cb->setModel( new QStandardItemModel(0, 1, cb) );
cb->model()->setObjectName( "New model" );
cb->addItem( "sample", "Sample" );
qDebug() << "Model name:" << cb->model()->objectName() << "Parent: " << cb->model()->parent();
To copy to clipboard, switch view to plain text mode
Bookmarks