pyqt: how to remove a widget?
Solution 1
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.
Solution 2
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).
Related videos on Youtube
Claudiu
Graduated from Brown University. E-mail: [email protected]
Updated on December 15, 2020Comments
-
Claudiu over 3 years
I have a
QGroupBox
widget with children in it that I want to remove. How do I do that? I can't find anyremoveWidget
,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.-
Admin about 13 yearsare you looking to just remove the widget from the QGroupBox, or do you want to delete the widget altogether?
-
-
Eli Bendersky about 13 yearsYou 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
-
Claudiu about 13 yearsi 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.
-
Eli Bendersky about 13 yearswell, you can use the
removeChild
method of the parent, but it's better tohide
-
Claudiu about 13 years@Eli: ah now we're talking! the problem is i don't see a
removeChild
method in the docs, and python says it doesn't exist when i try to call it on my qdialog... can you point me to where it is? -
Eli Bendersky about 13 yearssorry, 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
-
bobsbeenjamin over 3 yearsThis does the trick. Sure, the widget is created anyhow, and just hidden, but that's good enough for me.
-
trumpetlicks almost 2 yearsI 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.