要求
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/398e5f4d85536e456f8c29ac7f1213ce.png#pic_center)
头文件
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
#include <QDateTime>
#include <QDebug>
#include <QMouseEvent>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class myWidget; }
QT_END_NAMESPACE
class myWidget : public QWidget
{
Q_OBJECT
signals:
void mysignal();
public:
myWidget(QWidget *parent = nullptr);
~myWidget();
private slots:
void on_btn_start_clicked();
void on_timeout();
void on_btn_close_clicked();
private:
Ui::myWidget *ui;
QTimer *t;
QTextToSpeech *s ;
};
#endif
源代码
#include "mywidget.h"
#include "ui_mywidget.h"
myWidget::myWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::myWidget)
{
ui->setupUi(this);
t = new QTimer(this);
connect(t, &QTimer::timeout, this, &myWidget::on_timeout);
s = new QTextToSpeech(this);
}
myWidget::~myWidget()
{
delete ui;
}
void myWidget::on_btn_start_clicked()
{
t->start(1000);
ui->btn_start->setText("闹钟模式ing");
ui->btn_close->setText("关闭");
}
void myWidget::on_timeout()
{
QTime sysTime = QTime::currentTime();
QString time_str = sysTime.toString();
ui->lable_show->setText(time_str);
QFont ft;
ft.setPointSize(25);
ui->lable_show->setFont(ft);
if(time_str == ui->lineEdit_set->text())
{
s->say("闹钟响了闹钟响了");
}
}
void myWidget::on_btn_close_clicked()
{
ui->lineEdit_set->clear();
ui->btn_start->setText("启动");
ui->btn_close->setText("闹钟已关闭");
}
ui界面设置
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7762b7f3e553aa3641f0b8bc0546cfc3.png#pic_center)
结果显示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4b4ef27c4689b2e4200059cae6850983.png#pic_center)