cpp
复制代码
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTimer>
#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_sbtn_clicked();
void on_cbtn_clicked();
private:
Ui::Widget *ui;
int id;
QTextToSpeech *speech;
};
#endif // WIDGET_H
cpp
复制代码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(980,460);
speech = new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
//获取系统时间
QTime systime = QTime::currentTime();
//将系统时间转换
QString s = systime.toString("hh:mm:ss");
//将转换后的系统时间放入Label中
ui->timelabel->setText(s);
//居中显示
ui->timelabel->setAlignment(Qt::AlignCenter);
//比对闹钟时间和系统时间,如果相等,则播报闹钟
if(ui->tdeit->text() == s)
{
for(int i=0;i<3;i++)
{
speech->say(ui->speechlabel->text());
}
}
}
void Widget::on_sbtn_clicked()
{
id = startTimer(1000);
}
void Widget::on_cbtn_clicked()
{
killTimer(id);
}