C++实现对象树与QT利用按钮了解信号与槽

这篇博客介绍了如何手动实现对象树模型并利用Qt进行GUI编程。内容包括创建一个项目,其中包含三个按钮,第一个按钮用于播报第二个按钮的文本,播报后自动禁用;第二个按钮用于关闭整个项目;第三个按钮则用于恢复第一个按钮的可用状态。此外,还展示了使用信号与槽机制来处理按钮点击事件,以及使用QTextToSpeech进行语音播报。测试结果显示功能按预期运行。
摘要由CSDN通过智能技术生成
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值