I heard that the widgets should be allocated on the heap (using new), and then there are no needs to delete them (it is done automatically).
- Can someone explain why?
- What happens if a widget is not allocated that way, but on a stack?
I am not sure if it matters, but all widgets I am creating have a parent.
This says :
If parent is 0, the new widget becomes
a window. If parent is another widget,
this widget becomes a child window
inside parent. The new widget is
deleted when its parent is deleted.
There’s no magic involved. Simply put, a QObject automatically deletes its children in its destructor. So, as long as your widget has a parent and that you destroy that parent, you don’t have to worry about the children. So if you wondered what was that
QObject * parentparameter, well, that’s what it’s there for.Also, from the doc:
So, avoid giving parents to objects that are stack-allocated.