pyqt: how to remove a widget?

78,319

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).

Share:
78,319

Related videos on Youtube

Claudiu
Author by

Claudiu

Graduated from Brown University. E-mail: [email protected]

Updated on December 15, 2020

Comments

  • Claudiu
    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 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.

    • Admin
      Admin about 13 years
      are you looking to just remove the widget from the QGroupBox, or do you want to delete the widget altogether?
  • Eli Bendersky
    Eli Bendersky about 13 years
    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
  • Claudiu
    Claudiu about 13 years
    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.
  • Eli Bendersky
    Eli Bendersky about 13 years
    well, you can use the removeChild method of the parent, but it's better to hide
  • Claudiu
    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
    Eli Bendersky about 13 years
    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
  • bobsbeenjamin
    bobsbeenjamin over 3 years
    This does the trick. Sure, the widget is created anyhow, and just hidden, but that's good enough for me.
  • trumpetlicks
    trumpetlicks almost 2 years
    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.