Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory management for collections of widgets in Qt

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?

like image 354
J Cooper Avatar asked Mar 03 '10 18:03

J Cooper


2 Answers

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.

like image 147
Barry Wark Avatar answered Nov 20 '22 14:11

Barry Wark


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.

like image 5
Trey Stout Avatar answered Nov 20 '22 15:11

Trey Stout