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?
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?
Last edited by radek.z; 18th May 2009 at 08:30.
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.
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?
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.
Bookmarks