TheFuzz TheFuzz - 3 months ago 23
C++ Question

Qt library destructor Question

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
http://doc.trolltech.com/4.5/demos-mainwindow-mainwindow-cpp.html

and here is the .h file
http://doc.trolltech.com/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?

Answer

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.