Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Management in Qt

Tags:

c++

qt

qt4

nokia

I have small doubt about Qt memory management.

Let's take an example of Listview, in listview we add each item by allocating memory dynamically. So in this case do we need to delete all the "new"ed items manually.

E.g:

Qlistview *list = new Qlistview;
QStandardItemModel  *mModel = new QStandardItemModel();
list ->setModel(mModel);

for(int I =0;i<10;i++)
{
QsandardItem *item = new QsandardItem(“Hi”);
mModel->appendRow(item);
}

In this example, should item be deleted manually?

like image 794
Naruto Avatar asked Oct 26 '10 06:10

Naruto


1 Answers

QStandardItemModel takes ownership of items, so they will be automatically deleted when model is destroyed. You still need to delete the model itself (setModel() doesn't transfer ownership of model to the view, because one model can be used by multiple views).

like image 138
chalup Avatar answered Nov 05 '22 04:11

chalup