头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QTextToSpeech>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
QTimer* time;//一秒输出一次系统时间
QTextToSpeech *speecher;//读者
int id;//启动后1秒判断一次时间是否与系统时间相同
int id2;//相同后3秒一次读内容,读三次后关闭
void timerEvent(QTimerEvent *e);
private:
Ui::Widget *ui;
public slots:
void time_slots();
private slots:
void on_okbut_clicked();
void on_pushButton_clicked();
};
#endif // WIDGET_H
函数:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
,time(new QTimer(this))
,speecher(new QTextToSpeech(this))
, ui(new Ui::Widget)
{
ui->setupUi(this);
time->start(1000);
connect(time,&QTimer::timeout,this,&Widget::time_slots);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId()==id){
QTime t=QTime::currentTime();
QString s=t.toString("hh::mm::ss");
if(s==ui->lineEdit->text()){
id2=startTimer(5000);
speecher->say(ui->label->text());
}
}else if(e->timerId()==id2){
static int count=0;
if(count++<3){
speecher->say(ui->label->text());
}else{
killTimer(id2);
}
}
}
void Widget::time_slots()
{
QTime t=QTime::currentTime();
QString s=t.toString("hh::mm::ss");
ui->timelab->setText("系统时间:"+s);
ui->timelab->setAlignment(Qt::AlignCenter);
}
void Widget::on_okbut_clicked()
{
if(ui->okbut->text()=="启动"){
if(ui->lineEdit->text().size()>7){
ui->okbut->setText("取消");
id=startTimer(1000);
}else{
QMessageBox::critical(this,"错误","时间输入错误",QMessageBox::Ok);
ui->lineEdit->clear();
}
}else{
QMessageBox::information(this,"闹钟","闹钟已经取消!",QMessageBox::Ok);
ui->okbut->setText("启动");
ui->lineEdit->clear();
killTimer(id);
killTimer(id2);
}
}
void Widget::on_pushButton_clicked()
{
this->close();
}
现象: