Results 1 to 2 of 2

Thread: deleting a widget

  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default deleting a widget

    I have this code:
    Qt Code:
    1. void MainWindow::on_actionSomeButton_triggered()
    2. {
    3. QWidgetDerivedClass *wid = new QWidgetDerivedClass ;
    4. wid->show();
    5. }
    To copy to clipboard, switch view to plain text mode 

    If I create the widget with Qt::WA_DeleteOnClose, this will free the memory allocated with new operator, when the user closes the widget? If not, to free the memory, is it valid to do:

    Qt Code:
    1. QWidgetDerivedClass::~QWidgetDerivedClass()
    2. {
    3. delete this;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Just want to make share I'm not leaking memory.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: deleting a widget

    Well the docs states that:
    Makes Qt delete this widget when the widget has accepted the close event (see QWidget::closeEvent()).
    and for closeEvent():
    This event handler is called with the given event when Qt receives a window close request for a top-level widget from the window system.
    So, if this a top level widget you are talking about (it looks like it) then yes, Qt takes care of deleting it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 0
    Last Post: 19th October 2010, 19:45
  2. Replies: 7
    Last Post: 19th May 2010, 13:24
  3. QMdiSubWindows - Deleting
    By Kerubu in forum Newbie
    Replies: 1
    Last Post: 25th August 2009, 05:23
  4. Rarely crushing application when deleting widget
    By nedlab in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2009, 09:58
  5. deleting qaction
    By hyling in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2006, 21:48

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.