头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTextToSpeech>
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_bon_1_clicked();
private:
Ui::Widget *ui;
int tid;
QTime sys_time;
QTextToSpeech *speech;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
speech =new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId()==tid)
{
sys_time=QTime::currentTime();
QString t=sys_time.toString("hh:mm:ss");
ui->SysTime_lab->setText(t);
if(t==ui->TimeIn_edit->text())
{
for(int i=0;i<5;i++)
{
speech->say(ui->speek_lab->text());
}
}
}
}
void Widget::on_bon_1_clicked()
{
if(ui->bon_1->text()=="启动")
{
tid=startTimer(1000);
ui->bon_1->setText("关闭");
}
else
{
killTimer(tid);
ui->bon_1->setText("启动");
}
}
main.cp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}