QT-对象树模型及信号与槽函数实现

信号与槽函数实现

创建一个项目,提供三个按钮,第一个按钮实现播报第二个按钮的内容,播报结束后,设置自己不可用。第二个按钮的内容是关闭,实现功能是关掉整个项目,第三个按钮功能是将第一个按钮设置为可以状态

代码实现

.h头文件

#ifndef MYWND_H
#define MYWND_H

#include <QWidget>
#include<QPushButton>
#include<QtTextToSpeech>

class MyWnd : public QWidget
{
    Q_OBJECT
signals:
        void signal_1();
public slots:
    void shoeMes();//声明展示函数
    void say_mes();

public:
    MyWnd(QWidget *parent = nullptr);
    ~MyWnd();
    //定义一个按钮指针
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;
    //定义一个播报者
    QTextToSpeech speech;
};
#endif // MYWND_H

功能函数

#include "mywnd.h"
#include <QDebug>


void MyWnd::shoeMes(){
    //static int i = 1;
   //this->btn2->setText(QString::number(i++));
    speech.say(btn2->text());
    btn1->setEnabled(false);
}

//处理自定义信号的槽函数
void MyWnd::say_mes()
{
    speech.say(btn2->text());
}
MyWnd::MyWnd(QWidget *parent)
    : QWidget(parent)
{   
    this->resize(1024,768);//重新设置主窗口控件大小
    this->setMaximumSize(1500,1000);//设置窗口最大尺寸
    this->setMinimumSize(150,100);//设置窗口最小尺寸
    this->setFixedSize(1198,1102);//设置固定尺寸
    //设置窗口标题
    this->setWindowTitle("FirstWindow");
    //获取标题
    QString title = this->windowTitle();//得到窗口标题
    qDebug()<<title;
    //设置背景色
    this->setBackgroundRole(QPalette::Dark);
    this->setAutoFillBackground(true);
    //移动位置
    this->move(50,50);
    // 输出宽度和高度
    qDebug()<<"width = "<<this->width()<<"heigh = "<<this->height();

    //输出坐标点
    qDebug()<<"坐标点 "<<this->x()<<","<<this->y();
    qDebug()<<this->pos();


    btn1 = new QPushButton(this);
    btn1->setParent(this);
    btn1->resize(75,30);
    btn1->move(0,height()/2);
    btn1->setText("点击");

    btn2 = new QPushButton(this);
    btn2->move(btn1->width(),height()/2);
    btn2->setText("关闭");

    btn2->resize(btn1->size());

    btn3 = new QPushButton(this);
    btn3->move(btn1->width()+btn2->width(),height()/2);
    btn3->resize(btn1->size());
    btn3->setText("显示");

    //连接btn1发射的信号给showMes处理
    //connect(btn1,SIGNAL(clicked()),this,SLOT(shoeMes()));//Qt4

    //connect(btn1,&QPushButton::clicked,this,&MyWnd::shoeMes);//Qt5

    connect(btn1,&QPushButton::clicked,[&](){MyWnd::shoeMes();
    });
    connect(btn2,&QPushButton::clicked,[&](){MyWnd::close();
    });
    connect(btn3,&QPushButton::clicked,[&](){
        btn1->setEnabled(true);
        //emit signal_1();
    });
//    connect(this,&MyWnd::signal_1,[&](){    //将自定义信号连接到自定义槽
//        speech.say("");
//    });

//    connect(this,&MyWnd::signal_1,[&](){    //将自定义信号连接到自定义槽
//        btn2->setText(btn3->text());
//    });
//     connect(btn1, &QPushButton::clicked, this, &MyWnd::say_mes);

}

MyWnd::~MyWnd()
{
}


运行现象(部分)

在这里插入图片描述

手动实现对象树模型

代码实现

#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()
{

   
    B b;           //实例化b这个控件
    A *a = new A( &b );   //此时a依附于b

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值