PDA

View Full Version : qt smart pointer



sajis997
12th April 2011, 19:33
Hello forum,

I have declared a class as follows:





class Item : public QObject, QGraphicsItem
{

private:

//the following is turn also a sub-class of QObject
TextGraphicsItem *m_text;



};





Do i have to delete explicitly m_text in the destrucor or Qt will take care of it to delete the memory?



Thanks
Sajjad

stampede
12th April 2011, 19:43
QObject automatically deletes it's child objects, so if you pass a parent pointer to m_text constructor, it will be deleted if the parent is deleted.

Zlatomir
12th April 2011, 19:46
It depends:
If TextGraphicsItem isn't an QObject derived class, or if it doesn't have a parent (parent pointer is 0) you will need to delete it in ~Item().

If it take this (or other QObject pointer) as a parent you don't need to delete it manually, it will be deleted by the parent.

wysota
12th April 2011, 21:04
So far you just have a pointer so you don't have to delete anything. Once you initialize it with some object we can start talking about it.

Lykurg
12th April 2011, 21:59
Or use QScopedPointer and we don't need to talk about ;)