But no destructor of MyWidget being called unless I delete it explicitly.
Sure they are. For a demo:
#include <QtGui>
#include <QDebug>
{
Q_OBJECT
public:
setObjectName(name);
qDebug() << "Constructed" << objectName();
}
~MyWidget() {
qDebug() << "Destroyed" << objectName();
}
};
int main(int argc, char **argv) {
MyWidget *top = new MyWidget("top");
MyWidget *a = new MyWidget("a", top);
MyWidget *b = new MyWidget("b", top);
MyWidget *c = new MyWidget("c child of a", a);
delete top;
return 0;
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class MyWidget: public QWidget
{
Q_OBJECT
public:
MyWidget(const QString &name, QWidget *p = 0): QWidget(p) {
setObjectName(name);
qDebug() << "Constructed" << objectName();
}
~MyWidget() {
qDebug() << "Destroyed" << objectName();
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
MyWidget *top = new MyWidget("top");
MyWidget *a = new MyWidget("a", top);
MyWidget *b = new MyWidget("b", top);
MyWidget *c = new MyWidget("c child of a", a);
delete top;
return 0;
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Constructed "top"
Constructed "a"
Constructed "b"
Constructed "c child of a"
Destroyed "top"
Destroyed "a"
Destroyed "c child of a"
Destroyed "b"
Constructed "top"
Constructed "a"
Constructed "b"
Constructed "c child of a"
Destroyed "top"
Destroyed "a"
Destroyed "c child of a"
Destroyed "b"
To copy to clipboard, switch view to plain text mode
Perhaps in your real program you are not looking at or deleting the instances you think you are.
Bookmarks