PDA

View Full Version : Avoiding leaks with layout managers



invictus
21st March 2007, 18:20
I probably already know the answer to this, but I ask anyway;

Some examples in a book I am reading usually create like 4-5 layout managers like this:

QHBoxLayout *topLeftLayout = new QHBoxLayout;

This is done in the constructor of the widget and the pointer isnt stored anywhere for later reference. So basicly there is no way to do delete in the destructor. Is this common practice in QT or should I keep the pointer in the private section just so I can delete it in the destructor?

jpn
21st March 2007, 18:50
The layout manager becomes a child of the widget it is installed on so you don't need to delete it by hand.

invictus
21st March 2007, 18:53
The layout manager becomes a child of the widget it is installed on so you don't need to delete it by hand.

How about buttons, textfields, checkboxes, etc?

jpn
21st March 2007, 19:53
How about buttons, textfields, checkboxes, etc?
The layout manager handles reparenting of them. All widgets added to a layout become also children of the widget the layout is installed on.

invictus
21st March 2007, 20:13
So basicly I never have to delete any component that is displayed on another widget?

Will there be problems if I do delete something that is a child of a widget?

jpn
21st March 2007, 20:24
QObjects delete their children automatically and inform their parent when they get deleted. In addition, all relevant signal slot connections are also cleaned up. You are free to delete QObjects when you feel so with the exception that the sender of a signal must not be deleted in a slot.

Further reading: Object Trees and Object Ownership (http://doc.trolltech.com/4.2/objecttrees.html), notice QObject::dumpObjectTree() which is handy for examining object trees.

invictus
21st March 2007, 22:01
Thanks for the info!

I assume the QSocket and such need to be deleted though :p



Who needs Java garbage-collector anyway :p