Results 1 to 7 of 7

Thread: Avoiding leaks with layout managers

  1. #1
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Avoiding leaks with layout managers

    I probably already know the answer to this, but I ask anyway;

    Some examples in a book I am reading usually create like 4-5 layout managers like this:

    QHBoxLayout *topLeftLayout = new QHBoxLayout;

    This is done in the constructor of the widget and the pointer isnt stored anywhere for later reference. So basicly there is no way to do delete in the destructor. Is this common practice in QT or should I keep the pointer in the private section just so I can delete it in the destructor?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Avoiding leaks with layout managers

    The layout manager becomes a child of the widget it is installed on so you don't need to delete it by hand.
    J-P Nurmi

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

    invictus (21st March 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Avoiding leaks with layout managers

    Quote Originally Posted by jpn View Post
    The layout manager becomes a child of the widget it is installed on so you don't need to delete it by hand.
    How about buttons, textfields, checkboxes, etc?

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Avoiding leaks with layout managers

    Quote Originally Posted by invictus View Post
    How about buttons, textfields, checkboxes, etc?
    The layout manager handles reparenting of them. All widgets added to a layout become also children of the widget the layout is installed on.
    J-P Nurmi

  6. #5
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Avoiding leaks with layout managers

    So basicly I never have to delete any component that is displayed on another widget?

    Will there be problems if I do delete something that is a child of a widget?

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Avoiding leaks with layout managers

    QObjects delete their children automatically and inform their parent when they get deleted. In addition, all relevant signal slot connections are also cleaned up. You are free to delete QObjects when you feel so with the exception that the sender of a signal must not be deleted in a slot.

    Further reading: Object Trees and Object Ownership, notice QObject::dumpObjectTree() which is handy for examining object trees.
    J-P Nurmi

  8. #7
    Join Date
    Feb 2007
    Posts
    61
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Avoiding leaks with layout managers

    Thanks for the info!

    I assume the QSocket and such need to be deleted though



    Who needs Java garbage-collector anyway

Similar Threads

  1. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 20:21
  2. Resizing problems when applying a layout
    By JimBrown in forum Newbie
    Replies: 1
    Last Post: 21st February 2007, 22:54
  3. "dynamic" layout
    By hulk in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2006, 07:16
  4. Layout Managers???
    By jayw710 in forum Qt Programming
    Replies: 7
    Last Post: 18th February 2006, 03:42

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.