头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>//时间类
#include <QTimerEvent>//定时器事件类
#include <QDebug>
#include <QDateTime>//日期类
#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);
void read(); //声明阅读函数
private slots:
void on_Button_ture_clicked();
void on_Button_false_clicked();
private:
Ui::Widget *ui;
//定义定时器的标识
int timerId;
//记录时间
QString s1;
//记录文本
QString s2;
//实例化一个播报者
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);
//启动定时器
timerId = startTimer(1000);
//每隔一秒后,自动调用定时器处理函数,我们将逻辑写到定时器处理函数中
//设置关闭按钮不可用
ui->Button_false->setEnabled(false);
this->setFixedSize(600,300);
}
Widget::~Widget()
{
delete ui;
}
//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId()==timerId)
{
//调取系统时间,显示到页面中
QDateTime data = QDateTime::currentDateTime();//获取当前的系统日期
//将时间转换为字符串
QString strTime = data.toString("MM:dd-hh:mm:ss");
//将时间展示到页面中
ui->time_label->setFont(QFont("楷书",21));
ui->time_label->setText(strTime);
if(s1==strTime)
{
read();
// qDebug()<<s2;
}
}
}
//启动那妞触发事件处理函数
void Widget::on_Button_ture_clicked()
{
//设置关闭按钮可用
ui->Button_false->setEnabled(true);
//设置启动按钮不可用
ui->Button_ture->setEnabled(false);
//设置文本编辑器不可用
ui->Q_text->setEnabled(false);
//设置时间文本编辑器不可用
ui->time_line->setEnabled(false);
//获取输入的字符串
s1 = ui->time_line->text();
s2 = ui->Q_text->toPlainText();
// ui->time_line->clear();
// qDebug()<<s1;
}
void Widget::read()
{
// qDebug()<<s2<<"213";
for(int i=0;i<5;i++)
{
speecher.say(s2);
}
}
void Widget::on_Button_false_clicked()
{
//设置关闭按钮可用
ui->Button_false->setEnabled(false);
//设置启动按钮不可用
ui->Button_ture->setEnabled(true);
//设置文本编辑器不可用
ui->Q_text->setEnabled(true);
//设置时间文本编辑器不可用
ui->time_line->setEnabled(true);
}
UI
运行