.h
#ifndef MYCLOCK_H
#define MYCLOCK_H
#include <QWidget>
#include<QLCDNumber>
#include<QTime>
#include <QPushButton>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class MyClock; }
QT_END_NAMESPACE
class MyClock : public QWidget
{
Q_OBJECT
signals:
void singal_1();
public:
MyClock(QWidget *parent = nullptr);
~MyClock();
private slots:
void on_startBtn_clicked();
void on_StopBtn_clicked();
void timerEvent(QTimerEvent* e); //重写定时器事件
void on_hourBtn_clicked();
void on_minBtn_clicked();
void sayDeal();
private:
Ui::MyClock *ui;
//定义闪烁秒状态
int showFlag;
QString clockTime;
int clockFlag;
QTextToSpeech speech;
int sayflag=1;
};
#endif // MYCLOCK_H
.cpp
#include "myclock.h"
#include "ui_myclock.h"
#include<string>
#include<windows.h>
MyClock::MyClock(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MyClock)
{
ui->setupUi(this);
startTimer(1000);
ui->Lcd2->display("00:00");
clockTime = "00:00";
//speech.setRate(3);
connect(this, &MyClock::singal_1, this, &MyClock::sayDeal);
}
MyClock::~MyClock()
{
delete ui;
}
void MyClock::on_startBtn_clicked()
{
clockFlag=true;
}
void MyClock::on_StopBtn_clicked()
{
clockFlag=false;
}
void MyClock::timerEvent(QTimerEvent *)
{
//1.获取系统时间
QTime sysTime = QTime::currentTime();
//将系统时间转换为字符串
QString timeText = sysTime.toString("hh:mm");
if(showFlag)
{
timeText[2]=':';
clockTime[2]=':';
showFlag = false;
}else
{
timeText[2]=' ';
// clockTime[2]=' ';
showFlag = true;
}
//将时间字符串展示到LCD中
ui->Lcd1->display(timeText);
ui->Lcd2->display(clockTime);
if(clockFlag)
{
if(timeText==clockTime && sayflag==1)
{
emit singal_1();
}
}
}
void MyClock::on_hourBtn_clicked()
{
QString hour;
for(int i=0;i<2;i++)
{
hour[i]=clockTime[i];
}
int hh=hour.toInt();
hh++;
if(hh==24)
hh=0;
if(hh<10)
{
hour="0"+QString::number(hh);
}else{
hour=QString::number(hh);
}
for(int i=0;i<2;i++)
{
clockTime[i]= hour[i];
}
ui->Lcd2->display(clockTime);
}
void MyClock::on_minBtn_clicked()
{
QString mint;
for(int i=0;i<2;i++)
{
mint[i]=clockTime[3+i];
}
int mm=mint.toInt();
mm++;
if(mm==60)
mm=0;
if(mm<10)
{
mint="0"+QString::number(mm);
}else{
mint=QString::number(mm);
}
for(int i=0;i<2;i++)
{
clockTime[3+i]= mint[i];
}
ui->Lcd2->display(clockTime);
}
void MyClock::sayDeal()
{
int len = ui->textEdit->toPlainText().size();
int time =len/4;
speech.say(ui->textEdit->toPlainText());
Sleep(time*1000);
sayflag=1;
}
.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 \
myclock.cpp
HEADERS += \
myclock.h
FORMS += \
myclock.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target