QT基础-QT内存回收机制
参考Qt学习笔记(四):Qt 的内存回收机制 在 Qt 中,QObject 是顶层对象,当 Qt 程序关闭的时候,QObject 对象会被自动释放掉 mainwindow.cpp
#include "../Headers/mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
this->resize(1600, 1200);
btn1 = new Button1(this);
btn2 = new Button2();
}
MainWindow::~MainWindow() {
delete btn2;
qDebug() << "程序关闭,父被释放";
}
mainwindow.h
#ifndef QT_CREATOR_TEST_MAINWINDOW_H
#define QT_CREATOR_TEST_MAINWINDOW_H
#include <QWidget>
#include <QMainWindow>
#include <QDebug>
#include "../Headers/button1.h"
#include "../Headers/button2.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Button1 *btn1;
Button2 *btn2;
};
#endif
button1.h
#ifndef QT_CREATOR_TEST_BUTTON1_H
#define QT_CREATOR_TEST_BUTTON1_H
#include <QPushButton>
class Button1 : public QPushButton {
Q_OBJECT
public:
Button1(QWidget *parent = nullptr);
~Button1();
};
#endif
button1.cpp
#include "../Headers/button1.h"
#include <QDebug>
Button1::Button1(QWidget *parent) : QPushButton(parent) {
}
Button1::~Button1() {
qDebug() << "Button1析构函数启动";
}
button2.h
#ifndef QT_CREATOR_TEST_BUTTON2_H
#define QT_CREATOR_TEST_BUTTON2_H
#include <QPushButton>
class Button2 : public QPushButton {
Q_OBJECT
public:
Button2(QWidget *parent = nullptr);
~Button2();
};
#endif
button2.cpp
#include "../Headers/button2.h"
#include <QDebug>
Button2::Button2(QWidget *parent) : QPushButton(parent) {
}
Button2::~Button2() {
qDebug() << "Button2析构函数启动";
}
|