Results 1 to 6 of 6

Thread: Qt closeevent and garbage collection

  1. #1
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt closeevent and garbage collection

    Hi, is it necessary to call delete when a qt application is closed? For example I created several widgets.
    Are they automatically deleted when the closeEvent is executed?
    or is it necessary to call the destructor everytime the app is closed?

    Also, is there any effect in memory allocation when either of these forms are used?

    QWidget myWidget;
    QWidget *myWidget = new QWidget();

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    658
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt closeevent and garbage collection

    QWidget internally having a garbage collector .so no need to destruct as the base widget will automatically clean its child and child's children recursivly ..
    if u feel uncomfortable of not setting any widget as a parent to the base widget use QSharedPointer to declare that widget ..
    "Behind every great fortune lies a crime" - Balzac

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

    ehnuh (2nd November 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt closeevent and garbage collection

    Quote Originally Posted by ehnuh View Post
    Are they automatically deleted when the closeEvent is executed?
    No, closing a widget does not delete it. You can still query its members or even show it again.

    Since top-level widgets usually do not have parents assigned, you need to delete them yourself if you want them gone.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    ehnuh (2nd November 2012)

  6. #4
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt closeevent and garbage collection

    or use Qt::WA_DeleteOnClose
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    ehnuh (2nd November 2012)

  8. #5
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt closeevent and garbage collection

    the delete applies when creating the object using the new keyword. so if I declare my widget to be, QWidget BasicWidget.....this would automatically be deleted once I close my application?

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt closeevent and garbage collection

    Quote Originally Posted by ehnuh View Post
    the delete applies when creating the object using the new keyword. so if I declare my widget to be, QWidget BasicWidget.....this would automatically be deleted once I close my application?
    All memory is freed when you close your application, regardless how the object is created. It only matters if a destuctor of an object is called or not.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    ehnuh (4th November 2012)

Similar Threads

  1. QList and garbage collection
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 5th April 2012, 14:38
  2. Garbage collector - PyQt
    By janosimas in forum Newbie
    Replies: 0
    Last Post: 11th December 2011, 11:05
  3. Garbage collection
    By Septi in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 14:13
  4. QString, QStringList and garbage collection...
    By PaladinKnight in forum Newbie
    Replies: 4
    Last Post: 5th April 2010, 16:22
  5. question about garbage collection
    By Dumbledore in forum Qt Programming
    Replies: 4
    Last Post: 18th December 2007, 22:08

Tags for this Thread

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.