Results 1 to 4 of 4

Thread: QWidget *parent = 0 question

  1. #1
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QWidget *parent = 0 question

    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.

  2. #2
    Join Date
    May 2009
    Location
    Russia
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWidget *parent = 0 question

    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.

  3. #3
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QWidget *parent = 0 question

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget *parent = 0 question

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Non-transparent QWidget on semi-transparent parent
    By EuroElessar in forum Qt Programming
    Replies: 0
    Last Post: 29th August 2008, 16:20
  2. Enable QWidget child while parent disabled
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 30th May 2008, 10:53
  3. QWidget question
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 05:33
  4. Problem with Parent QWidget and Buttons not working
    By VireX in forum Qt Programming
    Replies: 7
    Last Post: 11th May 2007, 22:24
  5. Replies: 5
    Last Post: 4th August 2006, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.