将对象树模型手动实现一遍
#include <iostream>
#include <list>
class Obj;
using namespace std;
typedef list<Obj*> ChildList;
class Obj
{
public:
ChildList child;
Obj(Obj *parent=nullptr)
{
if(parent !=nullptr)
{
parent->child.push_back(this);
}
cout<<"Obj::构造函数"<<endl;
}
virtual ~Obj()
{
for(auto it = child.begin(); it!=child.end();it++)
{
delete *it;
}
cout<<"Obj::析构函数"<<endl;
}
};
class A:public Obj
{
public:
A(Obj *parent=nullptr)
{
if(parent!=nullptr)
{
parent->child.push_back(this);
}
cout<<"A::构造函数"<<endl;
}
~A()
{
cout<<"A::析构函数"<<endl;
}
};
class B:public Obj
{
public:
B(Obj *parent=nullptr)
{
if(parent!=nullptr)
{
parent->child.push_back(this);
}
cout<<"B::构造函数"<<endl;
}
~B()
{
cout<<"B::析构函数"<<endl;
}
};
int main()
{
A a;
B *b=new B(&a);
return 0;
}
将信号与槽函数的代码手动实现一遍
头文件>>
#ifndef MYFIRST_H
#define MYFIRST_H
#include <QWidget>
#include <QPushButton>
class myfirst : public QWidget
{
Q_OBJECT
public:
myfirst(QWidget *parent = nullptr);
~myfirst();
public slots:
void my_close();
void my_text();
void my_Text();
private:
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
};
#endif // MYFIRST_H
源文件>>
#include "myfirst.h"
#include <QDebug>
#include <QPushButton>
myfirst::myfirst(QWidget *parent)
: QWidget(parent)
{
this->setWindowTitle("my first window");
this->setMaximumSize(1024,512);
this->setMinimumSize(512,256);
//this->setFixedSize(500,500);
qDebug()<<this->windowTitle();
btn1=new QPushButton("btn1",this);
btn1->resize(100,50);
btn2=new QPushButton("btn2",this);
btn2->resize(btn1->size());
btn2->move(btn1->width(),0);
btn3=new QPushButton("btn3",this);
btn3->resize(btn1->size());
btn3->move(0,btn1->height());
connect(btn1,&QPushButton::clicked,this,&myfirst::my_close);
connect(btn3,&QPushButton::clicked,this,&myfirst::my_text);
//connect(btn3,&QPushButton::clicked,this,&myfirst::my_Text);
connect(btn3,SIGNAL(clicked()),this,SLOT(my_Text()));
}
myfirst::~myfirst()
{
}
void myfirst::my_close()
{
this->close();
}
void myfirst::my_text()
{
btn2->setText("world");
}
void myfirst::my_Text()
{
btn1->setText("hello");
}
主函数文件>>
#include "myfirst.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
myfirst w;
w.show();
return a.exec();
}