views:

172

answers:

2

Sorry for the dumb question, but I'm working with Qt and C++ for the first time, and working through the tutorial and some samples.

One thing that was mentioned was that Qt stuff doesn't need to be explicitly deleted. So, main question, does this also apply to collections of Qt stuff? Like say I want a dynamic number of MyWidgets, so I keep a vector or whatever of them. Are they still taken care of for me?

As a side question, what is going on to make it so I don't have to worry about destructors?

+7  A: 

The Qt memory management model is based upon a parent-child relationship. Qt classes take an optional parent as a parameter of their constructor. The new instance registers with this parent such that it is deleted when the parent is deleted. If you are using a Qt collection (e.g. QList), I believe you can set the list as the parent of its entries. If you're using an std::vector or other collection type, you will not get "automatic" memory management.

The Qt model makes a lot of sense in a UI hierarchy where it matches one-to-one with the UI hierarchy. In other cases, it doesn't always map as cleanly and you need to evaluate whether using the Qt system makes sense for the particular situation. The normal C++ tools still work: you can use std::tr1::shared_ptr or any of the other smart pointer classes to help you manage object lifetime. Qt also includes QPointer, a guarded pointer, and the QSharedPointer/QWeakPointer pair that implement a reference-couting smart pointer and weak-reference pair.

Barry Wark
You cannot set a Qt collection as the parent of an object. Objects that are dynamically allocated and have parent-child relationships are QObjects, and they take QObjects/QWidgets subclasses as parents. QList and the other collection classes do not inherit from QWidget (and by extension QObject), so you cannot set it as a parent.
blwy10
+4  A: 

Qt has an interesting object model for sure. When I first started it made me uneasy that there were so many new Foo calls and no deletes.

http://qt.nokia.com/doc/4.6/object.html Is a good place to start reading up on the object model.

Things of interest: QObject subclasses have their assignment and copy-ctor methods disabled. The chain of object child-parents is maintained internally by QObject.

Generally when instantiating a QObject subclass (if you don't plan on managing its pointer yourself) you will provide another QObject pointer as the parent. This 'parent' then takes over the management of the child you just made. You can call setParent() on a QObject to change who "owns" it. There are very few methods in Qt that will change the parent of an object, and they all explicitly state that they do in the docs.

So to answer your specific question: it depends on how you made all of your MyWidget instances.

If you made each one with a parent, then no you don't have to delete them. The parent will delete them when it gets deleted.

If you're keeping a QList<MyWidget*> collection of them, and you didn't give them a parent, then you should delete them yourself.

Trey Stout