Results 1 to 4 of 4

Thread: Question about layout and memory leak

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Question about layout and memory leak

    Hi to all,
    I know that when I create a new widget ( for example a QPushButton ) I need to pass the parent widget to the ctor, so the new created widget can be deleted when the parent is deleted.
    What is not so clear for me is ( looking at the documentation ) is why the parent is not passed to the widget when it's added to a layout.
    for example this code:

    Qt Code:
    1. QWidget *window = new QWidget;
    2. QPushButton *button1 = new QPushButton("One");
    3. QPushButton *button2 = new QPushButton("Two");
    4. QPushButton *button3 = new QPushButton("Three");
    5. QPushButton *button4 = new QPushButton("Four");
    6. QPushButton *button5 = new QPushButton("Five");
    7.  
    8. QVBoxLayout *layout = new QVBoxLayout;
    9. layout->addWidget(button1);
    10. layout->addWidget(button2);
    11. layout->addWidget(button3);
    12. layout->addWidget(button4);
    13. layout->addWidget(button5);
    14.  
    15. window->setLayout(layout);
    16. window->show();
    To copy to clipboard, switch view to plain text mode 

    is part of the QVBoxLayout documentation.
    I can not see where the parent widget is passed to the buttons and layout. Does it generate memory leaks errors?
    Maybe is my mistake or maybe the Qt developers doen't give much attention to the examples, it this last case it would be better that Qt developers post correct code without errors.

    Best Regards
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Question about layout and memory leak

    It's all ok. From the docs:
    void QLayout::addWidget ( QWidget * w )
    Adds widget w to this layout in a manner specific to the layout. This function uses addItem().
    -->
    void QLayout::addItem ( QLayoutItem * item ) [pure virtual]
    Implemented in subclasses to add an item. How it is added is specific to each subclass.
    This function is not usually called in application code. To add a widget to a layout, use the addWidget() function; to add a child layout, use the addLayout() function provided by the relevant QLayout subclass.
    Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.
    See also addWidget(), QBoxLayout::addLayout(), and QGridLayout::addLayout().
    and also
    void QWidget::setLayout ( QLayout * layout )
    Sets the layout manager for this widget to layout.
    If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.
    If layout is the layout manger on a different widget, setLayout() will reparent the layout and make it the layout manager for this widget.
    Example:
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(formWidget);
    setLayout(layout);
    An alternative to calling this function is to pass this widget to the layout's constructor.
    The QWidget will take ownership of layout.
    See also layout() and Layout Management.
    So at the end all buttons and the layout are children of the widget.

  3. #3
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about layout and memory leak

    From the docs:

    void QLayout::addWidget ( QWidget * w )

    Adds widget w to this layout in a manner specific to the layout. This function uses addItem().

    void QLayout::addItem ( QLayoutItem * item ) [pure virtual]

    Implemented in subclasses to add an item. How it is added is specific to each subclass.

    The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.
    Edit: Lykurg beat me to it!

  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about layout and memory leak

    Thanks to all,
    now is much more clear.
    Franco Amato

Similar Threads

  1. About the question of QTcpServer, there memory leak
    By hxj830088 in forum Qt Programming
    Replies: 4
    Last Post: 24th December 2009, 03:27
  2. Qt dll + memory leak
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2009, 13:28
  3. QList<pointer>::takeFirst() and memory leak question
    By AcerExtensa in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2009, 13:20
  4. memory leak question
    By cool_qt in forum General Programming
    Replies: 3
    Last Post: 20th January 2009, 07:49
  5. Memory leak
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 2nd September 2006, 15:31

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.