Paulo Facó
Paulo Facó

Reputation: 407

How to delete all the children of a Qt window?

I have a main window where I create the objects dynamically, so before making any new screen I have to first delete all the objects from the last screen. I was using this, and it was working great:

qDeleteAll(this->window->findChildren<QWidget*>());

But now I have this screen that has some QGraphicsView and QGraphicsScene, and I made their parent the main window as well, and now I got a crash when I try to delete all the main window's children.

So my question is basically, how can I avoid this crash? How can I easily and safely delete all my main window's children including GraphicsScene and GraphicsView?

Upvotes: 2

Views: 5336

Answers (1)

Chris
Chris

Reputation: 17545

I suspect that your crash is being caused by a double delete.

QObject's parent/child relationships make it so that if the parent is deleted, its children are deleted as well. So if you want to delete a widget and all of its descendants then all you need to do is delete the parent.

In your case I probably would make sure all of the dynamically created widgets belong to some kind of container widget/frame and delete that followed by recreating it.

Upvotes: 6

Related Questions