Qt编程--发现小知识点,析构Object类时,自动析构其子对象

在学习Qt时,发现,构造一个QObject对象A,并把另一个QObject对象B作为A的父对象传入,当析构对象A时,对象B也将被自动析构。


验证如下:

testobject1.h

#ifndef TESTOBJECT1
#define TESTOBJECT1
#include <QObject>
class TestObject : public QObject
{
    Q_OBJECT
public:
    explicit TestObject(QString objectName, QObject* parent = 0);
    ~TestObject();

    QString m_ObjectName;
};

#endif // TESTOBJECT1

testobject1.cpp

#include "testobject1.h"
#include <QDebug>


TestObject::TestObject(QString objectName, QObject* parent)
    :QObject(parent)
{
    m_ObjectName = objectName;
    qDebug()<<m_ObjectName<<"enter constuctor";
}

TestObject::~TestObject()
{
    qDebug()<<m_ObjectName<<"leave constuctor";
}


mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "testobject1.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    TestObject* m_testObject1;
    TestObject* m_testObject2;
public slots:
    void destroyTheObject();
};

#endif // MAINWINDOW_H




mainWindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"
#include <QDebug>
 
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //以下操作证明,将对父象传进去后,当父对象被析构时,其子对象也将被自动析构, 但不会自动为其赋NULL值。
 
 
    m_testObject1 = new TestObject("object1", this);
 
 
    m_testObject2 = new TestObject("object2", m_testObject1);
 
 
    connect(m_testObject2, &TestObject::destroyed, this, this->destroyTheObject);
 
 
//    if(m_testObject1)
//    {
//        delete m_testObject1;
//    }
 
 
//    if(NULL != m_testObject1)
//    {
//        qDebug()<<"the pointer of m_testObject1 is not null";
//    }
//    else
//    {
//        qDebug()<<"the pointer of m_testObject1 still  null";
//    }
 
 
//    if(NULL != m_testObject2)
//    {
//        qDebug()<<"the pointer of m_testObject2 is not null";
//    }
//    else
//    {
//        qDebug()<<"the pointer of m_testObject2 still  null";
//    }
 
 
    ui->setupUi(this);
}
 
 
MainWindow::~MainWindow()
{
    qDebug()<<"enter the main window destry function.";
    delete ui;
 
 
    if(NULL != m_testObject1)
    {
        qDebug()<<"the pointer of m_testObject1 is not null";
    }
    else
    {
        qDebug()<<"the pointer of m_testObject1 still  null";
    }
 
 
    if(NULL != m_testObject2)
    {
        qDebug()<<"the pointer of m_testObject2 is not null";
    }
    else
    {
        qDebug()<<"the pointer of m_testObject2 still  null";
    }
}
 
 
void MainWindow::destroyTheObject()
{
    if(m_testObject2)
    {
        qDebug()<<"make m_testObject2 to NULL";
        m_testObject2 = NULL;
    }
}
 
 


运行结果:

"object1" enter constuctor

"object2" enter constuctor

enter the main window destry function.

the pointer of m_testObject1 is not null

the pointer of m_testObject2 is not null

"object1" leave constuctor

"object2" leave constuctor

make m_testObject2 to NULL



猜你喜欢

转载自blog.csdn.net/humadivinity/article/details/51209698