#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
speecher = new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
//定时器超时时,自动执行的功能代码
void Widget::timerEvent(QTimerEvent *e)
{
//判断哪个定时器超时
if(e->timerId() == id){
//获取系统时间
QTime sys_time = QTime::currentTime();
//将时间转换成QString
QString s =sys_time.toString("hh:mm:ss");
//将获取的系统时间放入到label中
ui->xianshi_lab->setText(s);
//居中显示
ui->xianshi_lab->setAlignment(Qt::AlignCenter);
}
else if(e->timerId() == id2){
//判断时间和显示时间是否一致
if(ui->lineEdit->text() == ui->xianshi_lab->text()){
int i = 5;
while(i--){
speecher->say(ui->xuexi_lab->text());
}
}
}
}
//按钮对应的槽函数处理
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动"){
//启动一个定时器
id = startTimer(1000);
//启动第二个定时器
id2 = startTimer(1000);
//将按钮文本设置为“关闭”
ui->pushButton->setText("关闭");
}else{
ui->pushButton->setText("启动");
}
}
头文件
#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_pushButton_clicked();
private:
Ui::Widget *ui;
//定义一个定时器
int id;
int id2;
//定义一个语音播报者
QTextToSpeech *speecher;
};
#endif // WIDGET_H
运行结构:
思维导图: