TheFuzz TheFuzz - 5 months ago 58
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

and here is the .h file

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


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.