8.31-QT-作业

1、手动实现对象树模型

#include <iostream>
#include <list>

using namespace std;
class Tree
{
private:
    list<Tree*> childList;
public:
    Tree(Tree* p = nullptr)
    {
        if(p!=nullptr)
        {
            p->childList.push_back(this);
        }
        cout<<"Tree::构造函数"<<endl;
    }
    virtual ~Tree()
    {
        for(auto it=childList.begin();it!=childList.end();it++)
        {
            delete *it;
        }
        cout<<"Tree::析构函数"<<endl;
    }
    list<Tree*> child()
    {
        return childList;
    }
};

class A:public Tree
{
public:
    A(Tree* p = nullptr)
    {
        if(p!=nullptr)
        {
            p->child().push_back(this);
        }
        cout<<"A::构造函数"<<endl;
    }
    ~A()
    {
        cout<<"A::析构函数"<<endl;
    }

};
class B:public Tree
{
public:
    B(Tree* p = nullptr)
    {
        if(p!=nullptr)
        {
            p->child().push_back(this);
        }
        cout<<"B::构造函数"<<endl;
    }
    ~B()
    {
        cout<<"B::析构函数"<<endl;
    }

};

int main()
{
    B b1;
    A a1(&b1);
    A a2 = new A(&b1);

    return 0;
}

 

 

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

头文件

#ifndef MYWND_H
#define MYWND_H

#include <QWidget>
#include <QPushButton>
#include <QTextToSpeech>

class MyWnd : public QWidget
{
    Q_OBJECT
public slots:
    void msg();

public:
    MyWnd(QWidget *parent = nullptr);
    ~MyWnd();

    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;

    QTextToSpeech speech;
};
#endif // MYWND_H

mywnd.cpp

#include "mywnd.h"

void MyWnd::msg()
{
    speech.say(btn2->text());
    btn2->setEnabled(false);
}

MyWnd::MyWnd(QWidget *parent)
    : QWidget(parent)
{
    this->resize(1000,500);
    this->setFixedSize(1000,500);

    btn1 = new QPushButton("可用",this);
    btn1->resize(75,30);

    btn2 = new QPushButton("播报",this);
    btn2->resize(75,30);
    btn2->move(width()/2,height()/2);
    btn1->move(width()/2-btn2->width(),height()/2);
    btn3 = new QPushButton("关闭",this);
    btn3->resize(75,30);
    btn3->move(width()/2+btn2->width(),height()/2);

    connect(btn2,&QPushButton::clicked,this,&MyWnd::msg);
    connect(btn1,&QPushButton::clicked,[&](){
        btn2->setEnabled(true);
    });
    connect(btn3,&QPushButton::clicked,[&](){
        this->close();
    });

}

MyWnd::~MyWnd()
{
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值