QT练习1

1、手动实现对象树模型

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

作业1

代码:

#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;
    A *a = new A( &b );
    return 0;
}

作业2

代码:

mywnd.h文件

#ifndef MYWND_H
#define MYWND_H

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

class mywnd : public QWidget
{
    Q_OBJECT
signals:
    void signal_1();
public slots:
    void showMes();
    void say_mes();
    void my_clo();
    void start();
public:
    mywnd(QWidget *parent = 0);
    ~mywnd();

    //定义一个按钮指针
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;

    QTextToSpeech speech;

};

#endif // MYWND_H

mywnd.cpp文件

#include "mywnd.h"
#include <QDebug>
#include <QTextToSpeech>
#include <unistd.h>
void mywnd::showMes()
{
    static int i=1;
    this->btn2->setText(QString::number(i++));
}

void mywnd::start()
{
    btn2->setEnabled(true);
}

void mywnd::say_mes()
{
    speech.say(btn3->text());
    btn2->setEnabled(false);
}

void mywnd::my_clo()
{
    speech.say("帅哥再见");
    sleep(1);
    this->close();
}
mywnd::mywnd(QWidget *parent)
    : QWidget(parent)
{
    //界面大小
    this->resize(512,512);
    //this->setMaximumSize(1499,999);
    //this->setMinimumSize(1,1);
    //this->setFixedSize(1024,762);

    //设置窗口标题
    this->setWindowTitle("mywindow");

    //获取标题
    QString title = this->windowTitle();
    qDebug()<<title;

    //设置背景色
    this->setBackgroundRole(QPalette::Mid);
    this->setAutoFillBackground(true);

    //移动位置(左上为起点)
    //this->move(100,100);

    //输出界面宽度和高度
    qDebug()<<"width = "<<this->width()<<"height = "<<this->height();

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

    /*******************************/

    //设置父控件
    btn1 = new QPushButton();
    btn1->setParent(this);

    btn1->resize(100,50);
    btn1->move(width()/16-btn1->width()/16,height()/2);
    btn1->setText("重置");

    btn2 = new QPushButton(this);
    //btn2->setParent(this);
    btn2->resize(btn1->size());
    btn2->move(width()/3+btn2->width()/3,height()/2);
    btn2->setText("播报");

    btn3 = new QPushButton("帅哥你好",this);
    //btn3->setParent(this);
    btn3->resize(btn1->size());
    btn3->move(width()/2.2+1.5*btn3->width(),height()/2);

    connect(btn3,&QPushButton::clicked,this,&mywnd::my_clo);
    connect(btn1,&QPushButton::clicked,this,&mywnd::start);
    connect(this,&mywnd::signal_1,[&]()
    {
        btn2->setText(btn3->text());
    });
    connect(btn2,&QPushButton::clicked,this,&mywnd::say_mes);
}

mywnd::~mywnd()
{

}

main.cpp文件

#include "mywnd.h"
#include <QApplication>
#include <QTextToSpeech>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    mywnd w;
    w.show();

    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值