目录
作业
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 执行结果
按下第一个按钮:
按下第三个按钮:
按下第三个按钮:
程序直接退出,没有现象。