1.将QT项目中,每个文件的每行代码,自行注释一遍
.pro
#项目工程文件
QT += core gui network texttospeech
#工程所需的库和类
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#qt版本超过4的需要加上
CONFIG += c++11
#支持C++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
#工程源文件
SOURCES += \
main.cpp \
widget.cpp
#工程头文件
HEADERS += \
widget.h
#ui文件
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widget.h
#ifndef WIDGET_H //防止头文件重复包含
#define WIDGET_H
#include <QWidget> //引入QWidget所需要的头文件
#include<QPushButton> //引入QPushButton所需要的头文件
QT_BEGIN_NAMESPACE //开始定义命名空间
namespace Ui { class Widget; } //命名空间UI中的Widget类声明
QT_END_NAMESPACE //定义命名空间结束
class Widget : public QWidget //自定义图形化界面Widget类继承自Qwidget类
{
Q_OBJECT //信号和槽对应的元
signals: //信号
void change(); //信号的声明无需定义
public slots: // 槽
//void on_change_slot(); //声明 定义
public:
Widget(QWidget *parent = nullptr); //Widget类的无参构造的声明 nullptr属于C++中的NULL
~Widget(); //析构函数
private:
Ui::Widget *ui; //定义一个可以执行命名空间ui中的Widget部件的指针
QPushButton *btn1; //定义一个可以指向QPushButton部件的指针
};
#endif // WIDGET_H
main.cpp
#include "widget.h" //引入工程头文件
#include <QApplication> // 引入应用开发的头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //实例化一个应用程序的类
Widget w; //在栈区实例化一个Widget对象
w.show(); //调用Widget类中的show成员函数
return a.exec(); //轮询等待信号和事件
}
widget.cpp
#include "widget.h" //引入工程头文件
#include "ui_widget.h" //引入ui转化的头文件
Widget::Widget(QWidget *parent)// 自定义类的定义部分
: QWidget(parent) // 通过显性调用父类的有参构造给从父类中继承的成员属性初始化
, ui(new Ui::Widget) // 显性调用ui的无参构造实例化对象
{
ui->setupUi(this); // 通过ui指针设置部件 依附于父部件,调用其成员函数
this->btn1 = new QPushButton(this); //实例化自定义的按钮部件
this->btn1->setText("按钮"); //设置按钮部件的文本显示
ui->clickBtn->setText("按钮2"); //通过ui指针改变另一个按钮的文本显示
}
Widget::~Widget() //显性定义Widget类的析构函数
{
delete ui; //释放的ui空间
}
2.重新手动实现对象树模型
#include <iostream>
#include <list>
using namespace std;
class A
{
public:
list<A*> child;
public:
A(A *st = NULL)
{
if(st != NULL)
{
st->child.push_back(this);
}
}
virtual ~A()
{
for(auto p = child.begin(); p != child.end();p++)
delete *p;
}
};
class B:public A
{
public:
B(A *st = NULL)
{
if(st != NULL)
{
st->child.push_back(this);
}
}
~B()
{
cout << "B:析构函数" << endl;
}
};
class C:public A
{
public:
C(A* st = NULL)
{
if(st != NULL)
{
st->child.push_back(this);
}
}
~C()
{
cout << "C:析构函数" << endl;
}
};
int main()
{
B t;
C* t1 = new C(&t);
return 0;
}