You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.
You just use
delete layout;
like you would with any other pointer you created using new.
new