Results 1 to 2 of 2

Thread: QWidget *parent = 0 is just a default argument

  1. #1
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default QWidget *parent = 0 is just a default argument

    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

  2. #2
    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 is just a default argument

    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.
    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.


  3. The following user says thank you to wysota for this useful post:

    dotjan (28th February 2012)

Similar Threads

  1. Cancel effect from parent qwidget
    By Wong in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2011, 01:59
  2. Replies: 7
    Last Post: 12th January 2011, 22:01
  3. Promoting the parent QWidget of a QWidget form
    By extrakun in forum Qt Tools
    Replies: 6
    Last Post: 16th April 2010, 14:19
  4. QWidget *parent = 0 question
    By radek.z in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2009, 09:32
  5. default argument compiler warning
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2007, 14:57

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.