Results 1 to 8 of 8

Thread: How to destroy an object right

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to destroy an object right

    Hello!

    I have a widget subclassing QFrame and it has a layout and lots of other widgets inside it. I'm wondering how to code the destructor right.

    Qt Code:
    1. MyFrame::MyFrame(QWidget *parent) : QFrame(parent)
    2. {
    3. // Build inner parts.
    4. QFrame *header = new QFrame;
    5. QFrame *footer = new QFrame;
    6.  
    7. QHBoxLayout layout;
    8. layout.addWidget(header);
    9. layout.addWidget(footer);
    10. setLayout(&layout);
    11.  
    12. SomeOwnObject someOwnObject = new SomeOwnObject();
    13. }
    14.  
    15. MyFrame::~MyFrame()
    16. {
    17. if (someOwnObject != NULL)
    18. delete someOwnObject;
    19. }
    To copy to clipboard, switch view to plain text mode 

    I figure I do need to delete my own object. In the documentation it says that the widget takes ownership of the layout when setLayout(layout) is called and also that the layout takes ownership of a widget when layout->addWidget(widget) is called. So basicly I'm unsure if I somehow need to call the destructor of the superclass or maybe delete the layout.

  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: How to destroy an object right

    Quote Originally Posted by Cruz View Post
    I figure I do need to delete my own object.
    Yes, unless it's a QObject subclass and you pass a proper parent.

    In the documentation it says that the widget takes ownership of the layout when setLayout(layout) is called
    So why do you allocate it on the stack? It goes out of scope according to normal C++ rules, as soon as the constructor ends. You should allocate it on the heap.

    and also that the layout takes ownership of a widget when layout->addWidget(widget) is called.
    In fact layouts don't take ownership of widgets, but they will reparent them correctly so that "this" becomes the parent of "header" and "footer".

    So basicly I'm unsure if I somehow need to call the destructor of the superclass or maybe delete the layout.
    No, you don't need to delete the layout. QLayout is a QObject, and becomes a child of the widget when installed. Just allocate it on the heap as adviced.
    J-P Nurmi

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

    Cruz (22nd January 2009)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to destroy an object right

    Ok I understand the heap advice. What's still not clear to me is that if I call delete MyFrame somewhere, is the destructor of the superclass also called or do I have to do it manually?

    And if so, actually how? I tried QFrame::~QFrame() and it didn't work.
    Last edited by Cruz; 22nd January 2009 at 11:01. Reason: spelling error

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to destroy an object right

    Qt Code:
    1. ...
    2. delete myWidget;
    3. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    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: How to destroy an object right

    Quote Originally Posted by Cruz View Post
    Ok I understand the heap advice. What's still not clear to me is that if I call delete MyFrame somewhere, is the destructor of the superclass also called or do I have to do it manually?

    And if so, actually how? I tried QFrame::~QFrame() and it didn't work.
    See C++ FAQ Lite: [11.12] When I write a derived class's destructor, do I need to explicitly call the destructor for my base class?
    J-P Nurmi

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

    Cruz (22nd January 2009)

  8. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to destroy an object right

    I still don't have full control of it. I'm trying to clear a layout from widgets. In the documentation it says

    Qt Code:
    1. QLayoutItem * child;
    2. while ((child = layout()->takeAt(0)) != 0)
    3. {
    4. delete child;
    5. }
    To copy to clipboard, switch view to plain text mode 

    would safely clear a layout. When I use this, the widgets still stay on the screen. So I figure I need to cast the QLayoutItem* pointers to pointers to widgets of my kind, say MyWidget*. When I do that before deleting, the program crashes. Do I need to cast here actually?

    Before I saw this in the documentation, I used an approach based on removeWidget(widget), but for that I need to keep a list of pointers to the widgets I added to the layout. It would be much more elegant to rely on the list of pointers that the layout itself is keeping.

  9. #7
    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: How to destroy an object right

    QLayoutItem represents an item in the layout. It's not a pointer to the widget. See QLayoutItem::widget(). But what are you actually trying to do? These kind of layout modifications tend to be problematic in terms of flickering etc. Perhaps you could just use a QStackedWidget?
    J-P Nurmi

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

    Cruz (22nd January 2009)

  11. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to destroy an object right

    I have a lot of QFrames coming in from an external source. They are placed on a widget say in a frame pool, so that the user can select a few of them and drag them somewhere else. After that the pool is cleared and new frames are coming in. So I'm looking for a short way to clear out the pool and the takeAt() method came to my attention.

    Thanks for the hint, it works really well now. There is no flicker and I don't need to maintain an internal data structure to keep track of the frames.

Similar Threads

  1. Getting std::string object from QString object ( qt3)
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2013, 20:09
  2. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  3. A form returning an object.
    By cbarmpar in forum Qt Programming
    Replies: 3
    Last Post: 8th September 2008, 05:21
  4. Object Not destroy
    By Sudhanshu Sharma in forum Qt Programming
    Replies: 3
    Last Post: 30th July 2008, 23:16
  5. Open a QMainWindow Object in QDialog Object
    By chuengchuenghq in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2008, 06:33

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.