#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); speech=new QTextToSpeech(this); id1=startTimer(1000); //设置文本到中间 ui->sys_label->setAlignment(Qt::AlignCenter); ui->textEdit->setAlignment(Qt::AlignCenter); ui->setlineEdit->setAlignment(Qt::AlignCenter); } Widget::~Widget() { delete ui; } void Widget::timerEvent(QTimerEvent *e) { QTime sys_time=QTime::currentTime(); QString s=sys_time.toString("hh:mm:ss"); if(e->timerId()==id1)//每秒显示时间 { ui->sys_label->setText(s); } else if(e->timerId()==id2)//每秒对比是否到闹钟时间 { QString time=ui->setlineEdit->text(); QString text=ui->textEdit->toPlainText(); if(s==time) { int n=5; while(n--) { speech->say(text); qDebug()<<text; } } } } void Widget::on_btn_clicked() { if(ui->btn->text()=="启动") { ui->btn->setText("关闭"); id2=startTimer(1000); } else { killTimer(id2); ui->btn->setText("启动"); } }