1、手动实现对象树模型
2、创建一个项目,提供三个按钮,第一个按钮实现播报第二个按钮的内容,播报结束后,设置自己不可用。第二个按钮的内容是关闭,实现功能是关掉整个项目,第三个按钮功能是将第一个按钮设置为可以按
#include <iostream>
#include<list>
using namespace std;
class ObjTree
{
private:
list<ObjTree *> childList; //存放子对象的指针链表
public:
ObjTree(ObjTree *parent = nullptr) {
if(parent != nullptr)
parent->childList.push_back(this);
}
virtual ~ObjTree()
{
for (auto it=childList.begin(); it!=childList.end(); ++it) {
delete *it;
}
}
list<ObjTree *> & child()
{
return childList;
}
};
//定义子类
class A:public ObjTree
{
public:
A(ObjTree *parent = nullptr)
{
if(parent != nullptr)
{
parent->child().push_back(this);
}
cout<<"A::构造函数"<<endl;
}
virtual ~A()
{
cout<<"A::析构函数"<<endl;
}
};
class B:public ObjTree
{
public:
B(ObjTree *parent = nullptr)
{
if(parent != nullptr)
{
parent->child().push_back(this);
}
cout<<"B::构造函数"<<endl;
}
virtual ~B()
{
cout<<"B::析构函数"<<endl;
}
};
int main()
{
cout<<"================"<<endl;
B b; //实例化b这个控件
A *a = new A( &b ); //此时a依附于b
return 0;
}
#include "widget.h" #include<QDebug> #include <QTextToSpeech> //处理自定义信号的槽函数 void MyWnd::say_mes() { speech.say(btn2->text()); btn1->setEnabled(false); } MyWnd::MyWnd(QWidget *parent) : QWidget(parent) { //设置窗口标题 this->setWindowTitle("zuoyeWindow"); this->setFixedSize(500,300); //设置固定尺寸 btn1 = new QPushButton(); btn1->setParent(this); //设置父控件 btn1->resize(100,30); btn1->move(0, height()/2); btn1->setText("播报->"); btn2 = new QPushButton(this); btn2->move(btn1->width(), height()/2); btn2->resize(btn1->size()); btn2->setText("关闭"); btn3 = new QPushButton("恢复\"播报->\"",this); btn3->resize(btn1->size()); btn3->move(btn1->width()+btn2->width(), height()/2); connect(btn1, &QPushButton::clicked, this, &MyWnd::say_mes);//QT5版本的连接 // disconnect(btn1, &QPushButton::clicked, this, &MyWnd::showMes); //QT5版本的断开 connect(btn3, &QPushButton::clicked, [&](){ //使用Larmda表达式当做槽函数 btn1->setEnabled(true); }); connect(btn2,&QPushButton::clicked,[&](){ this->close(); }); } MyWnd::~MyWnd() { }