头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimerEvent>
#include <QTimer>
#include <QtTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void timeout_slot();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int id; //定时器的id
QTimer *timer;
//实例化一个语音播报者
QTextToSpeech *speecher;
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, timer(new QTimer)
{
ui->setupUi(this);
//连接timeout 和自定的槽函数
connect(timer, &QTimer::timeout, this, &Widget::timeout_slot);
timer->start(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timeout_slot()
{
//获取系统时间Qtime
QTime sys_time = QTime::currentTime(); //获取到当前系统时间
//将系统时间转换成字符串
QString s = sys_time.toString("hh:mm:ss");
//将系统时间设置到ui界面上的lab中
ui->sysTime->setText(s);
//居中
ui->sysTime->setAlignment(Qt::AlignCenter);
}
void Widget::on_pushButton_clicked()
{
QString alarm = ui->timeEdit->text();
if(alarm <= ui->sysTime->text())
{
while(1)
{
speecher->say(ui->label_3->text());
}
}
}
思维导图: