I have a QGroupBox
widget with children in it that I want to remove. How do I do that? I can't find any removeWidget
, removeChild
, removeItem
, or anything similar in the docs. I can only see how to remove things from a layout, but that, apparently, doesn't remove it from the actual widget.
-
are you looking to _just_ remove the widget from the QGroupBox, or do you want to delete the widget altogether? – May 06 '11 at 15:46
2 Answers
If your widget have no child widgets that depend on it i think you can use:
layout.removeWidget(self.widget_name)
self.widget_name.deleteLater()
self.widget_name = None
in my tests when it is a widget that have childs you have to:
import sip
layout.removeWidget(self.widget_name)
sip.delete(self.widget_name)
self.widget_name = None
if you don't have a variable name for the widget at class or global level you still can remove from layout with layout.takeAt(index) and get the widget pointer from the QLayoutItem this functions returns with QLayoutItem.widget() method, in that case you don't need to assign to None the variable name because it is not referenced outside your function.
Try both methods and see what works for you (don't leak memory after repeat a good bunch of times).

- 1,068
- 1
- 6
- 10
Well, this works: on the widget i want to remove, call widget.setParent(None)
. I like how adding to a layout adds a widget to the container, but removing from a layout doesn't... fun stuff.

- 224,032
- 165
- 485
- 680
-
You must understand that a widget is created anyway, whether you put it in a layout or not, and it has a parent. A layout is just responsible for organizing it on the screen, it's not a reliable or even usable method to show and hide widgets – Eli Bendersky May 05 '11 at 15:07
-
i don't see a way to add or remove a widget (any add/remove widget function) except for the layouts. adding the widget to a layout seems to make it be displayed (what it actually does is set the parent of the widget to the container the layout is for). i would expect that removing that same widget from the same layout would make it not be displayed (i.e. put it back in the same state as before i added it to the layout). but the only way to do that seems to be to set the widget's parent to None... which is not symmetrical at all. – Claudiu May 05 '11 at 15:13
-
well, you can use the `removeChild` method of the parent, but it's better to `hide` – Eli Bendersky May 05 '11 at 15:18
-
@Eli: ah now we're talking! the problem is i don't see a `removeChild` method in [the docs](http://doc.qt.nokia.com/latest/qwidget-members.html), and python says it doesn't exist when i try to call it on my qdialog... can you point me to where it is? – Claudiu May 05 '11 at 15:24
-
sorry, I can't find it now - maybe it's something old that left traces in google search but no longer exists. Personally I never used it – Eli Bendersky May 05 '11 at 15:40
-
This does the trick. Sure, the widget is created anyhow, and just hidden, but that's good enough for me. – bobsbeenjamin Sep 18 '20 at 19:18
-
I would recommend also doing a '''delete(widget)''' also. As it turns out, by setting the parent to None, if you do a widget.show(), the widget will come alive in it's own window. This stops your application from truly closing, and it is in general bad memory management practice. It is particularly bad when using different imaging views that tend to be memory hogs. – trumpetlicks Jun 07 '22 at 17:16