作业
pro文件:
QT += texttospeech
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton> //按钮类
#include <QLabel> //标签类
#include <QLineEdit> //行编译器类
#include <QMoveEvent>
#include <QTimer> //定时器类
#include <QTime> //时间类
#include <QWidget>
#include <QtTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void mouseMoveEvent(QMouseEvent *event) override; //鼠标移动事件
void mousePressEvent(QMouseEvent *event) override; //鼠标点击事件
private slots:
void btn1_clicked();
void btn2_clicked();
void timeout_slot();
private:
Ui::Widget *ui;
//定义一个定时器变量
QTimer t1;
QPushButton *btn1;
QPushButton *btn2;
QLabel *lab1;
QLineEdit *let1;
QLineEdit *let2;
QPoint temp; //中间辅助向量
};
#endif // WIDGET_H
主文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->resize(460,360);
this->setWindowTitle("My First Window"); //设置窗口名称
//设置 去除控件的头部
this->setWindowFlag(Qt::FramelessWindowHint);
this->move(300,400);
//按钮1
btn1 = new QPushButton("启动",this);
btn1->resize(100,30);
btn1->move(250,80);
connect(btn1,&QPushButton::clicked,this,&Widget::btn1_clicked);
//按钮2
btn2 = new QPushButton("取消",this);
btn2->resize(100,30);
btn2->move(btn1->x()+btn1->width()+10,btn1->y());
btn2->setEnabled(false); //设置开始不可用
connect(btn2,&QPushButton::clicked,this,&Widget::btn2_clicked);
//标签
lab1 = new QLabel(this);
lab1->resize(200,70);
lab1->move(40,30);
//行编译器
let1 = new QLineEdit(this);
let1->resize(210,30);
let1->move(250,30);
let2 = new QLineEdit(this);
let2->resize(400,210);
let2->move(40,110);
let2->setText("三更灯火五更鸡,"
"正是男儿读书时,"
"黑发不知勤学早,"
"白首方悔读书迟。");
//由于定时器事件的信号与槽绑定
connect(&t1,&QTimer::timeout,this,&Widget::timeout_slot);
}
Widget::~Widget()
{
delete ui;
}
//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos() - temp);
}
//鼠标点击事件
void Widget::mousePressEvent(QMouseEvent *event)
{
temp = event->globalPos() - this->pos();
if(event->button() == Qt::RightButton)
{
this->close();
}
}
void Widget::timeout_slot()
{
//获取系统的时间
QTime sysTime = QTime::currentTime();
//将QTime类对象转变成字符串
QString tm = sysTime.toString("hh:mm:ss");
//将时间展示到ui界面上
lab1->setText(tm);
//设置标签其中显示
lab1->setAlignment(Qt::AlignCenter);
}
//“启动”按钮
void Widget::btn1_clicked()
{
t1.start(1000); //每隔指定的时间发射一个timeout的信号
btn1->setEnabled(false); //使用期间数据不可更改
let1->setEnabled(false);
let2->setEnabled(false);
btn2->setEnabled(true);
//获取系统的时间
QTime sysTime = QTime::currentTime();
//将QTime类对象转变成字符串
QString tm = sysTime.toString("hh:mm:ss");
if(let1->text() == tm)
{
QTextToSpeech *Speecher=new QTextToSpeech;
Speecher->say(tm);
}
}
//“取消”按钮
void Widget::btn2_clicked()
{
t1.stop();
btn1->setEnabled(true);
let1->setEnabled(true);
let2->setEnabled(true);
btn2->setEnabled(false);
}