Results 1 to 2 of 2

Thread: Deleting widgets in the destructors

  1. #1
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Deleting widgets in the destructors

    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:

    Qt Code:
    1. ==5112== 4,360 (20 direct, 4,340 indirect) bytes in 1 blocks are definitely lost in loss record 35 of 217
    2. ==5112== at 0x40219DC: operator new(unsigned) (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
    3. ==5112== by 0x80BFCA7: CreateVirtualHostWizard::createTypePage() (createvirtualhostwizard.cpp:280)
    4. ==5112== by 0x80C01DC: CreateVirtualHostWizard::createPages() (createvirtualhostwizard.cpp:253)
    5. ==5112== by 0x80C2616: CreateVirtualHostWizard::CreateVirtualHostWizard(WorkerThread*, QWidget*) (createvirtualhostwizard.cpp:93)
    6. ==5112== by 0x8069C31: MainWindow::createVirtualHost() (mainwindow.cpp:351)
    7. ==5112== by 0x80C4633: MainWindow::qt_metacall(QMetaObject::Call, int, void**) (moc_mainwindow.cpp:121)
    8. ==5112== by 0x494D505: QMetaObject::activate(QObject*, int, int, void**) (qobject.cpp:2674)
    9. ==5112== by 0x494D6D1: QMetaObject::activate(QObject*, QMetaObject const*, int, int, void**) (qobject.cpp:2721)
    10. ==5112== by 0x41185AD: QAction::triggered(bool) (moc_qaction.cpp:176)
    11. ==5112== by 0x41198DA: QAction::activate(QAction::ActionEvent) (qaction.cpp:898)
    12. ==5112== by 0x43FE79E: QMenuPrivate::activateAction(QAction*, QAction::ActionEvent) (qmenu.cpp:666)
    13. ==5112== by 0x44010AD: QMenu::mouseReleaseEvent(QMouseEvent*) (qmenu.cpp:1726)
    To copy to clipboard, switch view to plain text mode 

    Any idea what could it be?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting widgets in the destructors

    Quote Originally Posted by vfernandez
    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.

    Quote Originally Posted by vfernandez
    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.

Similar Threads

  1. QLogText & QLogTable : 2 widgets to display text log
    By fcoiffie in forum Qt-based Software
    Replies: 7
    Last Post: 28th April 2019, 07:52
  2. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28
  3. Replies: 11
    Last Post: 7th July 2006, 13:09
  4. Creating Widgets
    By hylke in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2006, 08:37

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.