1.
头代码
#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; //定时器返回id
int id2;
QString s;
QTextToSpeech *speecher; //创建一个语音播报者
};
#endif // WIDGET_H
源代码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
id = startTimer(1000); //每隔1s执行功能代码
speecher = new QTextToSpeech(this);
this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
}
void Widget::timerEvent(QTimerEvent *e)
{
//判断计时器id
if(e->timerId() == id)
{ //获取当前时间
QTime sys_time = QTime::currentTime();
s = sys_time.toString("hh:mm:ss");
//将获取时间放入label中
ui->sys_lab->setText(s);
//设置居中显示
ui->sys_lab->setAlignment(Qt::AlignCenter);
}else if(e->timerId() == id2)
{
if(ui->lineEdit->text() == s)
{
for(int i=0;i<5;i++)
{
speecher->say(ui->label_2->text());
}
//关闭定时器
killTimer(id2);
ui->pushButton->setText("启动");
ui->lineEdit->clear();
}
}
}
Widget::~Widget()
{
delete ui;
}
//启动按钮对应槽函数处理
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动")
{
//启动定时器
id2 = startTimer(1000);
ui->pushButton->setText("关闭");
}else {
//关闭定时器
killTimer(id2);
ui->pushButton->setText("启动");
}
}
效果图
2.思维导图