PDA

View Full Version : QWidget *parent = 0 question



radek.z
18th May 2009, 08:11
Hi,
I have a question what is purpose of a default initialization for QWidget *parent = 0 in a constructor of any widget? Should it always be there? Is there any exceptions?

motodrug
18th May 2009, 08:57
This is because every QWidget needs the pointer to a parent QWidget passed as a formal parameter.
Generally speaking, if you create a main window (QMainWindow) it doesn't need a parent. But in case of pop-up window (say QDialog), the parent is essential to control their relationship: user input, modality, visibility, alignment, etc.

radek.z
18th May 2009, 09:25
Hi,
yeah this I know, but my question is why in all examples in qt documentation and book is this pointer initialized to 0 by default. What if I know that the widget has to has a parent, should I also use this default initialization? If yes why?

wysota
18th May 2009, 09:32
This default value is so that you can omit it if you create the widget (possibly on stack) with no parent. If your item can live without a parent (and this is probably always the case) then you should follow the convention and allow the parameter to be omitted for your object as well.