一、将对象树模型手动实现一遍
#include <iostream>
#include <list>
using namespace std;
class obj;
typedef list<obj*> childlist;
//定义基类
class obj
{
public:
childlist children;
obj(obj* parent=NULL)
{
if(parent!=nullptr)
{
parent->children.push_back(this);
}
}
virtual ~obj()
{
for(childlist::iterator it=children.begin();it!=children.end();it++)
{
delete *it;
}
}
};
//定义子类
class A:public obj
{
public:
A(obj * parent=nullptr)
{
if(nullptr!=parent)
{
parent->children.push_back(this);
}
}
~A()
{
cout<<"A的析构"<<endl;
}
};
//定义子类
class B:public obj
{
public:
B(obj * parent=nullptr)
{
if(nullptr!=parent)
{
parent->children.push_back(this);
}
}
~B()
{
cout<<"B的析构"<<endl;
}
};
int main()
{
A a;
B *b = new B(&a);
delete b;
return 0;
}
二、 将信号与槽函数的代码手动实现一遍
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
public:
Widget(QWidget *parent = nullptr);
~Widget();
//定义关于btn1的槽函数
void page_change();
void settex();
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include <iostream>
#include <QDebug>
#include <QPushButton>
#include <unistd.h>
using namespace std;
//有参构造
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setWindowTitle("window of the world");
qDebug()<<this->windowTitle();
//设置窗口大小
this->setFixedSize(1000,1000);
//设置透明度
this->setWindowOpacity(0.9);
btn1 = new QPushButton ("login",this);
btn1->setFixedSize(200,100);
btn1->move(450,200);
btn2 = new QPushButton ("check",this);
btn2->setFixedSize(200,100);
btn2->move(450,400);
btn3 = new QPushButton ("launch",this);
btn3->setFixedSize(200,100);
btn3->move(450,600);
//qt5版本 信号与槽
connect(btn1,&QPushButton::clicked,this,&Widget::page_change);
// connect(btn2,SIGNAL(clicked()),this,SLOT(settex()));
connect(btn2,&QPushButton::clicked,this,&Widget::settex);
}
Widget::~Widget()
{
}
void Widget::page_change()
{
btn2->setText("checking....");
}
void Widget::settex()
{
btn3->setText("launched...");
}