PDA

View Full Version : Question about layout and memory leak



franco.amato
15th October 2010, 18:22
Hi to all,
I know that when I create a new widget ( for example a QPushButton ) I need to pass the parent widget to the ctor, so the new created widget can be deleted when the parent is deleted.
What is not so clear for me is ( looking at the documentation ) is why the parent is not passed to the widget when it's added to a layout.
for example this code:



QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);

window->setLayout(layout);
window->show();

is part of the QVBoxLayout documentation.
I can not see where the parent widget is passed to the buttons and layout. Does it generate memory leaks errors?
Maybe is my mistake or maybe the Qt developers doen't give much attention to the examples, it this last case it would be better that Qt developers post correct code without errors.

Best Regards

Lykurg
15th October 2010, 18:32
It's all ok. From the docs:

void QLayout::addWidget ( QWidget * w )
Adds widget w to this layout in a manner specific to the layout. This function uses addItem().
-->
void QLayout::addItem ( QLayoutItem * item ) [pure virtual]
Implemented in subclasses to add an item. How it is added is specific to each subclass.
This function is not usually called in application code. To add a widget to a layout, use the addWidget() function; to add a child layout, use the addLayout() function provided by the relevant QLayout subclass.
Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.
See also addWidget(), QBoxLayout::addLayout(), and QGridLayout::addLayout().

and also

void QWidget::setLayout ( QLayout * layout )
Sets the layout manager for this widget to layout.
If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.
If layout is the layout manger on a different widget, setLayout() will reparent the layout and make it the layout manager for this widget.
Example:
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(formWidget);
setLayout(layout);
An alternative to calling this function is to pass this widget to the layout's constructor.
The QWidget will take ownership of layout.
See also layout() and Layout Management.


So at the end all buttons and the layout are children of the widget.

Timoteo
15th October 2010, 18:33
From the docs:


void QLayout::addWidget ( QWidget * w )

Adds widget w to this layout in a manner specific to the layout. This function uses addItem().

void QLayout::addItem ( QLayoutItem * item ) [pure virtual]

Implemented in subclasses to add an item. How it is added is specific to each subclass.

The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.

Edit: Lykurg beat me to it!

franco.amato
15th October 2010, 18:50
Thanks to all,
now is much more clear.