Results 1 to 2 of 2

Thread: to free or not to free

  1. #1
    Join Date
    Apr 2006
    Location
    the netherlands
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question to free or not to free

    Hi all,

    I am new to this forum and it looks like a great resource for Qt information. I am used to Delphi so it's Qt as well as C++ I'll have to learn but it sure looks good what Trolltech did!

    Now to the question.

    I am not sure on the memory stuff. What is automagically destroyed and what is not?

    Here is a piece of code of my test project:

    Qt Code:
    1. VerticalDialog::VerticalDialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. VerticalItemModel *vertical = new VerticalItemModel;
    5. lytMain = new QGridLayout(this);
    6.  
    7. lytSurface = new QHBoxLayout();
    8. lytOkCancel = new QHBoxLayout();
    9. lytOpenSave = new QHBoxLayout();
    10.  
    11. //table
    12. tblVertical = new QTableView();
    13. tblVertical->setAlternatingRowColors(true);
    14. tblVertical->setModel(vertical);
    15. ...
    To copy to clipboard, switch view to plain text mode 

    Do I need to free the memory myself (for example for the QHBoxLayout classes) or does Qt take care of this? I do not assign a parent to them so there is nobody `to take care of them'??

    Thanks in advance,
    Rob

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: to free or not to free

    Quote Originally Posted by masoroso
    What is automagically destroyed and what is not?
    Every QObject which has a parent will be destroyed when its parent is destroyed.

    Quote Originally Posted by masoroso
    I do not assign a parent to them so there is nobody `to take care of them'??
    Well... some object might adopt them.

    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.
    When you use a layout, you don't need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.
    Important: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.
    You can nest layouts using addLayout() on a layout; the inner layout then becomes a child of the layout it is inserted into. The Basic Layouts example uses this feature to create a complex dialog.

  3. The following 2 users say thank you to jacek for this useful post:

    acix (20th April 2006), masoroso (17th April 2006)

Similar Threads

  1. little logical question........
    By sudheer in forum Qt Tools
    Replies: 3
    Last Post: 8th May 2008, 14:15
  2. What to free or not to free, that is the question
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 05:04
  3. Announce: Free Pascal / Lazarus for Qt
    By sekelsenmat in forum Qt-based Software
    Replies: 0
    Last Post: 10th October 2006, 13:28
  4. how to find the free space on a drive
    By klaus1111 in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2006, 10:25

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
  •  
Qt is a trademark of The Qt Company.