头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTimerEvent>
#include<QTime>
#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 timerEvent(QTimerEvent *e);
private slots:
void on_btn1_clicked();
void on_btn2_clicked();
private:
Ui::Widget *ui;
bool ok=true;
QTextToSpeech speecher;
int timeid;
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(800,600);
this->startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
QTime systime =QTime::currentTime();
QString t=systime.toString("hh:mm");
if(e->timerId()!=timeid)
{
if(ok==true)
{
t[2]=':';
ok=false;
}else
{
t[2]=' ';
ok=true;
}
ui->lcdNumber->display(t);
}else if(e->timerId()==timeid)
{
if(t==ui->lineEdit->text())
{
killTimer(timeid);
speecher.say(ui->textEdit->toPlainText());
timeid=startTimer(5000);
}
}
}
void Widget::on_btn1_clicked()
{
if(ui->lineEdit->text()!="")
{
timeid=startTimer(1000);
ui->btn1->setEnabled(false);
ui->lineEdit->setEnabled(false);
}
}
void Widget::on_btn2_clicked()
{
killTimer(timeid);
if(ui->lineEdit->text()!="")
{
ui->btn1->setEnabled(true);
ui->lineEdit->setEnabled(true);
ui->lineEdit->clear();
}
}
主函数:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}