Results 1 to 3 of 3

Thread: "new" + "delete" a QWidget and its children

  1. #1
    Join Date
    Aug 2009
    Posts
    13
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default "new" + "delete" a QWidget and its children

    If I understand the QWidget documentation correctly, there is no need to delete children widgets when deleting the parent widget.

    QWidget::QWidget ( QWidget * parent = 0, Qt::WindowFlags f = 0 )

    Constructs a widget which is a child of parent, with widget flags set to f.

    If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is deleted.
    QWidget::~QWidget ()

    Destroys the widget.

    All this widget's children are deleted first. The application exits if this widget is the main widget.
    However, I wrote the following test program, where I intentionally delete the children widgets in the parent's destructor and it deleted everything just fine. So it seems that I do need to explicitly delete the children widgets before deleting the parent widget, else it might lead to memory leak. But then I don't understand what the quoted QWidget documentation above. Can someone please explain to me what's the correct way? Thanks a bunch!

    Qt Code:
    1. #include <QApplication>
    2. #include <QtAlgorithms>
    3. #include <QMainWindow>
    4. #include <QtGui/QLabel>
    5. #include <QtGui/QTabWidget>
    6.  
    7. class B : public QWidget
    8. {
    9. public:
    10. B() : mLabel(new QLabel(this))
    11. {
    12. printf("B constructor\n");
    13. }
    14.  
    15. ~B()
    16. {
    17. printf("B destructor delete label\n");
    18. delete mLabel;
    19. }
    20.  
    21. private:
    22. QLabel* mLabel;
    23. };
    24.  
    25.  
    26. class A : public QMainWindow
    27. {
    28. public:
    29. A() :
    30. mLabel(new QLabel(this)),
    31. mTab(new QTabWidget(this)),
    32. mB(new B())
    33. {
    34. printf("A constructor\n");
    35. mTab->addTab(mB, "B");
    36. }
    37.  
    38. ~A()
    39. {
    40. printf("A destructor delete label\n");
    41. delete mLabel;
    42. printf("A destructor delete B\n");
    43. delete mB;
    44. printf("A destructor delete tab\n");
    45. delete mTab;
    46. }
    47.  
    48. private:
    49. QLabel* mLabel;
    50. QTabWidget* mTab;
    51. B* mB;
    52.  
    53. };
    54.  
    55. int main(int argc, char *argv[])
    56. {
    57. QApplication app(argc, argv);
    58.  
    59. A* a = new A();
    60. delete a;
    61.  
    62. return app.exec();
    63. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "new" + "delete" a QWidget and its children

    What is more likely happening is that when you delete the child, the child emits the destroyed() signal in its destructor and thus the parent removes that child from its list of children.

    So you can free them, or you can let the parent objects free there childs. It's upto you. I know which I'd prefer however.

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

    vkincaid (19th January 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: "new" + "delete" a QWidget and its children

    It is a feature of Qt that deleting QObjects is usually safe. If you connect two objects together using signals and slots and delete on of those, the connection is disconnected before so you have not dangling connections.
    It's nice to be important but it's more important to be nice.

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

    vkincaid (21st January 2010)

Similar Threads

  1. Replies: 3
    Last Post: 29th August 2009, 23:24
  2. Replies: 0
    Last Post: 3rd December 2008, 12:58
  3. Replies: 3
    Last Post: 8th July 2008, 20:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 16:58

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.