From the documentation of QWidget::setLayout():
No part of the code you posted deletes the existing layout, if any.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.
Bookmarks