PDA

View Full Version : Qt closeevent and garbage collection



ehnuh
31st October 2012, 06:54
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();

wagmare
31st October 2012, 07:24
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 ..

wysota
31st October 2012, 08:21
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.

amleto
31st October 2012, 16:27
or use Qt::WA_DeleteOnClose

ehnuh
2nd November 2012, 14:52
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?

wysota
2nd November 2012, 16:36
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.