form.cpp?
#include "form2.h"
#include "ui_form2.h"
#include <QDebug>
Form2::Form2(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form2)
{
ui->setupUi(this);
}
Form2::~Form2()
{
qDebug()<<"~Form2";
delete ui;
}
widget.cpp?
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "try1.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
f=new Form2(this);
}
Widget::~Widget()
{
qDebug()<<"~Widget";
delete f;
qDebug()<<"---";
delete ui;
}
?main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
结果:(f通过delete f进行析构)
~Widget
~Form2
---
另一种写法:?
widget.cpp
Widget::~Widget()
{
qDebug()<<"~Widget";
qDebug()<<"---";
delete ui;
}
结果:(f通过对象树进行析构)
~Widget
---
~Form2
分析:先调用了Widget的析构函数。然后会通过对象树进行析构!?