PDA

View Full Version : QWidget *parent = 0 is just a default argument



dotjan
27th February 2012, 19:03
Hi, just a confirm from you experts to see if I get this right, I can find many different comments on this but just want to double check it if what I have in mind is aligned...

In the constructor of my classes I always have (QWidget *parent = 0) as default argument, so that when I create an instance of that class in the heap:

- if I do not specify any parent, then the default 0 is applied and the new widget has no parent. So I must delete it manually
- if I do specify a parent, than this is deleted when I delete the parent object.

My guess is also that it is common to specify (this) as parent and, generally speaking, the instance without a parent should be created in the stack..

Thanks,
Jan

wysota
27th February 2012, 20:09
Yes, it's all correct. Maybe with a little exception to the last statement -- it's safest to always create widgets on heap since you don't know if somebody won't reparent it later on or delete it explicitely.