7
votes

i created one QWidget(Parent). in the inside of the parent widget i created another one QWidget(Child). In the run time i need to remove the child widget. how to do that?

i am not using any layout. i am directly putting in the Parent Widget.

Please Help me to fix this.

2

2 Answers

7
votes

If you add the widget with e.g.:

QWidget *w = new QWidget(parent);

...then you can remove it with:

delete w;

Another approach would be to just hide it:

w->hide();
2
votes

This answer is for those arriving from search engines and want an answer to the question as stated in the title.

If you want to remove a child from a parent without deleting it or hiding it (which does NOT remove it from its parent), set the child's parent to NULL.

QWidget::setParent(NULL)

Note that explicitly reparenting a widget like this carries several implications (e.g visibility automatically set to hidden). See QWidgets documentation for more information.