Important: Please read the Qt Code of Conduct -

Redrawing Widget after replacing containing Widget

  • I have a Widget (Parent) that has a layout in which another Widget (Child) is used. At some point, I need to replace the Child Widget by a new one and the Parent Widget needs to be redrawn. Naively, I wrote the following code as part of the Parent Widget:

    delete Child;
    Child = new ChildWidget();

    The problem is that the new Child is not drawn and instead the whole Child Widget area is no longer part of the layout. How can I solve this?

  • you have yo add the new ChildWidget to the layout. You don't need to call repaint either

  • Lifetime Qt Champion


    You're creating a new widget in the "void". You should put it in place of the old one. For example, if Child was in a layout, put the new object there.

    If you must replace it like that, why not have a "reset" function in ChildWidget ? That might be less aggressive.

  • Ok, how can I get the position in the Layout of Parent widget?

    I can do a reset as in this case the new Child concerns a completely different widget...

  • Ok guys, I found a solution thanks to your hints. I store the Layout and add the new Child after the old Child is deleted. This works perfectly well :)

Log in to reply