PDA

View Full Version : Deleting widgets in the destructors



vfernandez
2nd August 2006, 15:11
I'm a bit confused on which widgets should I delete manually and which ones are automatically deleted by QT when a parent widget is destroyed. Could someone bring me light on this issue?

For example, if I have a QStackedWidget and I add a few QWidgets to it, should I explicitly delete the child widgets in the destructor or does QT do it by itself?

I have a QDialog I'm using as a wizard (since QT4 doesn't provide any class to create wizards). In it, I'm using a QStackedWidget and in the class destructor of the wizard I don't delete any child widget of the QStackedWidget (only my own data). Valgrind complaints about memory blocks not freed in the methods that create the widgets (I'm not using QT Designer for this). Here's one of them:


==5112== 4,360 (20 direct, 4,340 indirect) bytes in 1 blocks are definitely lost in loss record 35 of 217
==5112== at 0x40219DC: operator new(unsigned) (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==5112== by 0x80BFCA7: CreateVirtualHostWizard::createTypePage() (createvirtualhostwizard.cpp:280)
==5112== by 0x80C01DC: CreateVirtualHostWizard::createPages() (createvirtualhostwizard.cpp:253)
==5112== by 0x80C2616: CreateVirtualHostWizard::CreateVirtualHostWizard(W orkerThread*, QWidget*) (createvirtualhostwizard.cpp:93)
==5112== by 0x8069C31: MainWindow::createVirtualHost() (mainwindow.cpp:351)
==5112== by 0x80C4633: MainWindow::qt_metacall(QMetaObject::Call, int, void**) (moc_mainwindow.cpp:121)
==5112== by 0x494D505: QMetaObject::activate(QObject*, int, int, void**) (qobject.cpp:2674)
==5112== by 0x494D6D1: QMetaObject::activate(QObject*, QMetaObject const*, int, int, void**) (qobject.cpp:2721)
==5112== by 0x41185AD: QAction::triggered(bool) (moc_qaction.cpp:176)
==5112== by 0x41198DA: QAction::activate(QAction::ActionEvent) (qaction.cpp:898)
==5112== by 0x43FE79E: QMenuPrivate::activateAction(QAction*, QAction::ActionEvent) (qmenu.cpp:666)
==5112== by 0x44010AD: QMenu::mouseReleaseEvent(QMouseEvent*) (qmenu.cpp:1726)

Any idea what could it be?

jacek
2nd August 2006, 15:26
I'm a bit confused on which widgets should I delete manually and which ones are automatically deleted by QT when a parent widget is destroyed. Could someone bring me light on this issue?
Every widget which has a parent will be destroyed when its parent is destroyed.


For example, if I have a QStackedWidget and I add a few QWidgets to it, should I explicitly delete the child widgets in the destructor or does QT do it by itself?
If you use QStackedWidget::addWidget() or similar method you don't have to.