Search code examples
c++qtdestructor

Qt library MainWindow destructor


Im new to the Qt library and i was going through the demonstrations. I came across this class without a destructor....

this is the cpp file https://docs.huihoo.com/qt/4.5/demos-mainwindow-mainwindow-cpp.html

and here is the .h file https://docs.huihoo.com/qt/4.5/demos-mainwindow-mainwindow-h.html

the constructor uses the new operator but the class doesn't have a destructor. Am I missing something?


Solution

  • Yes you are. Qt provides parent-child relationship. When a QObject is deleted, it deletes all of its children automatically.

    In the line below, a QTextEdit is created with this pointer as its parent.

    center = new QTextEdit(this); 
    

    So, when the parent (MainWindow) is deleted, center is automatically deleted too. Take a look at the QObject documentation.