头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimer>
#include <QTimerEvent>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
QString time1, time2;
QTextToSpeech * speech;
private slots:
void timeout();
void on_btn_clicked();
private:
Ui::Widget *ui;
QTimer *t;
int count = 0;
int id1, id2;
void timerEvent(QTimerEvent *e);
};
#endif // WIDGET_H
源代码:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
id1 = startTimer(1000);
//去除多余窗口
this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
t = new QTimer(this);
connect(t,&QTimer::timeout,this,&Widget::timeout);
speech = new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
//讲述人
void Widget::timeout()
{
// if(time1 == time2)
// {
// speech->say(ui->lab3->text());
// speech->say(ui->lab3->text());
// speech->say(ui->lab3->text());
// speech->say(ui->lab3->text());
// speech->say(ui->lab3->text());
// }
}
//系统时间 lab1
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == id1)
{//获取系统时间
QTime sys_tem = QTime::currentTime();
//转换类型
time1 = sys_tem.toString("hh::mm::ss");
//在lab1上呈现文本
ui->lab1->setText(time1);
//居中显示
ui->lab1->setAlignment(Qt::AlignCenter);
if(time1 == time2)
{
for(int i=0; i<5; i++)
{
speech->say(ui->lab3->text());
}
}
}
// killTimer(id1);
}
void Widget::on_btn_clicked()
{
if(ui->btn->text() == "启动")
{
// //启动定时器
// t->start(1000);
//改变文本
ui->btn->setText("暂停");
//绑定闹钟时间
time2 = ui->lineEdit->text();
}else{
// //关闭定时器
// t->stop();
killTimer(id1);
//改变文本
ui->btn->setText("启动");
}
}