[Qt-interest] Memory Management in Qt

Andre Somers andre at familiesomers.nl
Tue Oct 26 08:34:06 CEST 2010


Op 26-10-2010 8:17, Ramesh schreef:
> Thanks Rohan,
>
> What about the Qlistview and QStandardItemModel object?
> Should I delete them in the destructor of widget? Or any other alternative is
> there for it also?
There is an alternative for them too. You really should read up on 
QObject parent/child relationships. From the QObject documentation:

QObjects 
<http://doc.qt.nokia.com/4.7/qtwebkit-bridge.html#qobjects>organize 
themselves inobject trees 
<http://doc.qt.nokia.com/4.7/objecttrees.html>. When you create a 
QObject with another object as parent, the object will automatically add 
itself to the parent'schildren 
<http://doc.qt.nokia.com/4.7/qobject.html#children>() list. The parent 
takes ownership of the object; i.e., it will automatically delete its 
children in its destructor. You can look for an object by name and 
optionally type usingfindChild 
<http://doc.qt.nokia.com/4.7/qobject.html#findChild>() orfindChildren 
<http://doc.qt.nokia.com/4.7/qobject.html#findChildren>().

That means that if you create the model using a pointer to the parent 
object (for instance, the form you're displaying your list on, or 
perhaps the list view itself), it will be automatically deleted when the 
parent is deleted. The QListView itself must be parented to the form it 
is on, or it will act as the main widget of a form itself. That means 
that you don't have to manually delete widgets on a form manually.

Qt does not completely free you of memory management, but it is not 
often that you need to manually delete instances of Qt classes. Still, 
there are cases where you do. Keep an eye on the documentation. If it 
mentions things like "this object takes ownership of its children", then 
you are no longer responsible for managing those children.

André

> -----Original Message-----
> From: Rohan McGovern [mailto:rohan.mcgovern at nokia.com]
> Sent: Tuesday, October 26, 2010 11:45 AM
> To: ext Ramesh
> Cc: qt-interest at trolltech.com; QtS60-feedback at trolltech.com
> Subject: Re: [Qt-interest] Memory Management in Qt
>
> Ramesh said:
>> HI all,
>> I have small doubt about Qt memory management,
>>
>> Lets 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..
>>
>> Eg:
>>
>> 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, item should be deleted manually?
> No - the model takes ownership of the item, it'll be deleted when the
> model is deleted.
>
> There is an exception if you use the take* functions, though - these
> will cause the model to release ownership of items.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101026/70c669d3/attachment.html 


More information about the Qt-interest-old mailing list