QT->对象树模型和第一个GUI界面->day1

 

目录

一、对象树模型

1.1 代码

  1.2 执行结果 

 二、gui界面

2.1 代码

2.1.1 day.pro

2.1.2 mainwindow.h

2.1.3  main.cpp

2.1.4 mainwindow.cpp 

2.2  执行结果


作业

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

一、对象树模型

1.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()
{
    cout << "Hello World!" << endl;
    A a;
    B *b=new B(&a);
    return 0;
}

  1.2 执行结果 


 二、gui界面

2.1 代码

2.1.1 day.pro

2.1.2 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

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

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public slots:
    void show_btn1(); //声明展示函数
    void show_btn2();
    void show_btn3();

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

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

2.1.3  main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

2.1.4 mainwindow.cpp 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include <QTextToSpeech>
#include <unistd.h>

void MainWindow::show_btn1()
{
    speech.say(btn2->text());
    sleep(1);
    btn1->setEnabled(false);
}
void MainWindow::show_btn2()
{
    this->close();
}
void MainWindow::show_btn3()
{
    btn1->setEnabled(true);
}


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->resize(1024, 720);

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

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

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

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

    btn2 = new QPushButton();
    btn2->setParent(this);
    btn2->move(btn1->width(), height()/2);
    btn2->resize(btn1->size());
    btn2->setText("鸡你太美");

    btn3 = new QPushButton();
    btn3->setParent(this);
    btn3->resize(75,30);
    btn3->setText("复原");
    btn3->move(btn1->width()+btn2->width(), height()/2);

    connect(btn1,&QPushButton::clicked,this,
            &MainWindow::show_btn1);
    connect(btn2,&QPushButton::clicked,this,
            &MainWindow::show_btn2);
    connect(btn3,&QPushButton::clicked,this,
            &MainWindow::show_btn3);
}

MainWindow::~MainWindow()
{
    delete ui;
}

2.2  执行结果

按下第一个按钮:

按下第三个按钮:

 按下第三个按钮:

程序直接退出,没有现象。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值