头文件:
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent> //时间事件
#include <QTime> //时间类
#include <QTextToSpeech> //文本转语音
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *e);
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int id,id2;
QString s;
QTextToSpeech *speech;
};
#endif // WIDGET_H
源文件:
cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget),
speech(new QTextToSpeech (this))
{
ui->setupUi(this);
QTime sys_time = QTime::currentTime();//获取当前系统时间
s = sys_time.toString("hh:mm:ss");
ui->systimelabel->setText(s);
ui->systimelabel->setAlignment(Qt::AlignCenter);//居中显示
id = startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == id)
{
QTime sys_time = QTime::currentTime();
s = sys_time.toString("hh:mm:ss");//获取当前系统时间
ui->systimelabel->setText(s);
ui->systimelabel->setAlignment(Qt::AlignCenter);
}
else if(e->timerId() == id2)
{
if(ui->systimelabel->text() == ui->lineEdit->text())
{
for (int i = 0; i < 5; i++)
{
ui->label->setText("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
speech->say("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
}
}
}
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动")
{
id2 = startTimer(1000);
ui->pushButton->setText("关闭");
}else {
ui->pushButton->setText("启动");
killTimer(id2);
}
}