1 、手动实现对象树模型2 、创建一个项目,提供三个按钮,第一个按钮实现播报第二个按钮的内容,播报结束后,设置自己不可用。第二个按钮的内容是关闭,实现功能是关掉整个项目,第三个按钮功能是将第一个按钮设置为可以状态
利用按钮了解信号与槽
通过connect捕获按钮clicked产生的信号,并调用槽函数或者在connect中利用lambda函数在之后编写槽函数来完成预计功能
代码示例:
.h文件
#ifndef NODE_H
#define NODE_H
#include <QWidget>
#include <QPushButton>
#include <QTextToSpeech>
class node : public QWidget
{
Q_OBJECT
signals:
void signal_1();
public slots:
void show_mes();
public:
node(QWidget *parent = nullptr);
~node();
QPushButton *btn1;
QPushButton *btn3;
QPushButton *btn2;
QTextToSpeech speech;
};
#endif // NODE_H
.cpp
#include "node.h"
#include <QDebug>
#include <QPushButton>
#include <QTextToSpeech>
void node::show_mes()
{
speech.say(btn2->text());
btn1->setFlat(false);
}
node::node(QWidget *parent)
: QWidget(parent)
{
this->resize(1024,768); //重新设置主控件大小
this->setMaximumSize(1500,1000); //设置最大尺寸
this->setMinimumSize(500,200); //设置最小尺寸
//this->setFixedSize(); //设置固定尺寸
//设置窗口标题
this->setWindowTitle("first");
//获取标题
QString title = this->windowTitle(); //得到窗口标题
qDebug()<<title;
//设置背景色
// this->setAutoFillBackground(true); //允许设置背景色
// this->setBackgroundRole(QPalette::Dark);
//设置主控件位置
this->move(300,100);
//输出宽度和高度
qDebug()<<"width="<<this->width()<<" heigh="<<this->height()<<'\n';
//输出坐标点
qDebug()<<"坐标点 X:"<<this->x()<<" Y:"<<this->y()<<'\n';
/******************************************************************************/
btn1 = new QPushButton(this);
btn1->resize(80,40);
btn1->move(0,height()/2-50);
btn1->setText("播报");
btn2 = new QPushButton(this);
btn2->resize(80,40);
btn2->move(btn1->width(),height()/2-50);
btn2->setText("退出");
btn3 = new QPushButton("复原",this);
btn3->resize(80,40);
btn3->move(btn1->width()+btn2->width(),height()/2-50);
//连接btn1发射的信号给show_mes处理
// connect(btn1,SIGNAL(clicked()),this,SLOT(show_mes())); //QT4版本
// connect(btn1,&QPushButton::clicked,this,&node::show_mes); //QT5版本
connect(btn2,&QPushButton::clicked,[=](){
this->close();
});
connect(btn1,&QPushButton::clicked,[&](){ //播报按钮2内容,并将自己置于不可用状态
speech.say(btn2->text());
btn1->setEnabled(false);
});
connect(btn3,&QPushButton::clicked,[&](){ //复原按钮1
btn1->setEnabled(true);
});
}
node::~node()
{
}
测试结果:
按下播放按钮后播报按钮退出按钮中的文本并且执行后结果如图1所示,播报按钮被置于不可用状态。按下复原后播报按钮重新置于可用状态。
图1
图2
手动实现对象树模型:
代码示例:
#include <iostream>
#include <list>
using namespace std;
class ObjTree
{
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::create"<<endl;
}
~A(){
cout<<"A::close"<<endl;
}
};
class B:public ObjTree
{
public:
B(ObjTree *parent =nullptr){
if(parent!=nullptr)
{
parent->child().push_back(this);
}
cout<<"B::create"<<endl;
}
~B(){
cout<<"B::close"<<endl;
}
};
int main()
{
B b; //实例化b控件
A *a = new A(&b); //此时A依附与B
return 0;
}