How to remove QWidgets from QSplitter

Giancarlo picture Giancarlo · Dec 16, 2008 · Viewed 12.3k times · Source

In my app have a window splitted by a QSplitter, and I need to remove an widget.

How can I do that? I can't find useful methods

Answer

Michael Bishop picture Michael Bishop · Dec 19, 2008

It's not clear to me if you want to preserve the widget and put it somewhere else, or if you want to destroy the widget.

  • Destroying the widget: If you can get a pointer to the widget, you can simply delete it. The splitter will safely be notified that its child is being deleted and will remove it from itself.

  • Preserving the widget: If you grab the pointer to the widget, you can simply set its parent to some other widget and add it to another widget's layout and it will show up there. This is safe because the QSplitter will be notified that one of its children is being reparented.

If you want to set the parent to NULL (cjhuitt's answer) be aware that you are now responsible for cleaning up that memory because the widget no longer has a parent.