app.cpp
#include "app.h"
#include "ui_app.h"
int k1 = true;
APP::APP(QWidget *parent):QWidget(parent),ui(new Ui::APP)
{
ui->setupUi(this);
this->resize(380,300);
this->setStyleSheet("background-color:cyan;");//设置样式
speecher = new QTextToSpeech(this);
T1 = new QTimer(this);//定时器T1
T1->start(1000);
connect(T1,&QTimer::timeout,this,&APP::time_slot1);
connect(ui->SB1,&QPushButton::clicked,this,&APP::on_SB1start_clicked);
connect(ui->SB2,&QPushButton::clicked,this,&APP::on_SB2stop_clicked);
ui->Lb1->setAlignment(Qt::AlignCenter);//标签文本对齐方式 居中
ui->Lb1->setFont(QFont("华文行楷",20));
ui->Line1->setAlignment(Qt::AlignCenter);//对中
ui->Line1->setFont(QFont("华文行楷",16));//字体
ui->Text1->setAlignment(Qt::AlignCenter);//对中
ui->Text1->setFont(QFont("华文行楷",16));//字体
}
APP::~APP()
{
delete ui;
}
void APP::on_SB1start_clicked()//Start按钮触发事件
{
//启动后时间文本编辑器、文本编辑器和启动按钮将不可用
ui->SB1->setEnabled(false);
ui->Line1->setEnabled(false);
ui->Text1->setEnabled(false);
timer_T2 = new QTimer(this);//定时器T2
T1->start(100);
connect(T1,&QTimer::timeout,this,&APP::time_slot2);
}
void APP::on_SB2stop_clicked()//Stop按钮触发事件
{
ui->SB1->setEnabled(true);
ui->Line1->setEnabled(true);
ui->Text1->setEnabled(true);
}
void APP::time_slot1()//获取SYS时间显示
{
QTime sys_time = QTime::currentTime();//获取系统时间
QString t = sys_time.toString("hh:mm:ss");//时间转成字符串
ui->Lb1->setText(t);//时间字符串t=>ui.lab中
if(k1 == true)
{
ui->Line1->setText(ui->Lb1->text());
k1 = false;
}
}
void APP::time_slot2()//计时触发操作
{
if(ui->Line1->text() == ui->Lb1->text())
{
speecher->say(ui->Text1->toPlainText());//语音播放内容
}
}
app.h
#ifndef APP_H
#define APP_H
#include <QWidget>
#include <QTime> //时间类
#include <QTimer> //定时器类
#include <QTextToSpeech>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class APP; }
QT_END_NAMESPACE
class APP : public QWidget
{
Q_OBJECT
public:
APP(QWidget *parent = nullptr);
~APP();
void time_slot1();
void time_slot2();
private slots:
void on_SB1start_clicked();
void on_SB2stop_clicked();
private:
Ui::APP *ui;
QTimer *T1;
QTimer *timer_T2;
QTextToSpeech *speecher;
};
#endif // APP_H
t2.pro
QT += core gui texttospeech
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
app.cpp
HEADERS += \
app.h
FORMS += \
app.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target