8
votes

Let's consider we have a QWidget and a QLayout named general_layout that contains other widgets and layouts. general_layout is set as the QWidget layout like this:

setLayout(general_layout)

Now I should to change the content of QWidget. How can I do that? I have tried to delete and create a new layout for the QWidget and that new layout set as a layout of the QWidget, but could not complete my intentions successfully.

This is my code:

delete general_layout;
general_layout = new QHBoxLayout;
general_layout->addLayout(some_layout);
myQWidget->setLayout(general_layout);
1
Could you be more specific about your problem? What you are doing seems to be correct (ie delete old layout before setting a new one). - mtvec
Sure! I do the following: delete general_layout; general_layout = new QHBoxLayout; general_layout->addLayout(some_layout); myQWidget->setLayout(general_layout); - Narek

1 Answers

15
votes

The problem is that the widgets of a layout are not destroyed when deleting a layout. This results in all child widgets of myQWidget still being present, be it without a layout.

The solution is simple: add a

qDeleteAll(myQWidget->children());

after

delete general_layout;