QT作业(0831-林雪阵)

1.

#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的构造函数"<<endl;
    }
    ~A(){
        cout<<"A的析构函数"<<endl;
    }
};

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


int main()
{

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


    return 0;
}

 2

.pro

QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    homework.cpp

HEADERS += \
    homework.h


# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

.h

#ifndef HOMEWORK_H
#define HOMEWORK_H

#include <QMainWindow>
#include <QPushButton>
#include <QTextToSpeech>



class homework : public QMainWindow
{
    Q_OBJECT
signals:
    void singal_1();

public slots:
    void showMsg();
    void say_mes();

public:
    homework(QWidget *parent = nullptr);
    ~homework();
    //
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;

    //
    QTextToSpeech speech;

};
#endif // HOMEWORK_H

.cpp

#include "homework.h"
#include "ui_homework.h"

void homework::showMsg()
{

}

void homework::say_mes()
{
    speech.say("啊哈哈哈哈,鸡汤来喽");
    homework::btn2->setEnabled(false);
}

homework::homework(QWidget *parent)
    : QMainWindow(parent)
{
    this->resize(1000,800);
    this->setMaximumSize(1500,1000);
    this->setMinimumSize(500,200);
    this->setFixedSize(1000,800);

    //设置窗口标题
    this->setWindowTitle("故事会");
    //设置窗口位置
    this->move(100,100);
    /*********************设置按钮大小,位置***********************/
    btn1 = new QPushButton(this);
    //btnl->setParent(this);    //设置父空间
    btn1->resize(100,40);
    btn1->move(0,height()/2);
    btn1->setText("复用");

    btn2 = new QPushButton(this);
    btn2->move(btn1->width()+50,height()/2);
    btn2->resize(btn1->size());
    btn2->setText("开讲");

    btn3 = new QPushButton("关闭",this);
    btn3->resize(btn1->size());
    btn3->move(btn1->width()+btn2->width()+100,height()/2);

    /*********************设置按钮大小,位置***********************/
    //让'开讲' 按钮讲故事,并使按钮使能关闭
    connect(btn2,&QPushButton::clicked,this,&homework::say_mes);
    //开启'开讲'
    connect(btn1,&QPushButton::clicked,[=](){
        btn2->setEnabled(true);
    });
    //关闭窗口
    connect(btn3,&QPushButton::clicked,[=](){
        this->close();
    });


}

homework::~homework()
{
}

main

#include "homework.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    homework w;
    w.show();
    return a.exec();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林某某..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值