要求:
结果图:
clock.pro:
QT += core gui
QT += 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 \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent> //定时器事件处理函数
#include <QTime> //时间类
#include <QPushButton> //按钮类头文件
#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); //要重写的关于定时器事件处理函数的声明
private slots:
void on_start_Btn_clicked();
void on_stop_Btn_clicked();
private:
Ui::Widget *ui;
static int count; //定义静态变量
QTextToSpeech *speecher; //定义一个播报者
//定义一个基于事件处理的定时器id
int tid1;
int tid2;
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
tid1 = startTimer(1000); //启动一个定时器,隔1s会自动执行timerevent函数
//设置占位符
ui->clock_lineEdit->setPlaceholderText("输入多少秒后开启闹铃");
speecher = new QTextToSpeech(this);
}
int Widget::count = 0;
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == tid1)
{
//获取系统事件
QTime sys_time = QTime::currentTime(); //QTime类对象
//将时间转化为字符串
QString t = sys_time.toString("hh:mm:ss");
//将字符串展示到ui界面
ui->time_lab->setText(t);
ui->time_lab->setAlignment(Qt::AlignHCenter);
}else if(event->timerId() == tid2)
{
count++;
//将clock_lineEdit 的文本转化为整数
int m = ui->clock_lineEdit->text().toInt();
if(count == m)
{
//进行语音播报
speecher->say(ui->read_textEdit->toPlainText());
//停止计时器
killTimer(tid2);
}
}
}
void Widget::on_start_Btn_clicked()
{
//将start_Btn设置成不可用状态
ui->start_Btn->setEnabled(false);
//将clock_lineEdit设置成不可用状态
ui->clock_lineEdit->setEnabled(false);
//将stop_Btn设置成可用状态
ui->stop_Btn->setEnabled(true);
//启动第二个计时器
tid2 = startTimer(1000); //启动一个定时器,隔1s会自动执行timerevent函数
//将count置零
count = 0;
}
void Widget::on_stop_Btn_clicked()
{
//停止语音播报
speecher->stop();
//将stop_Btn设置成不可用状态
ui->stop_Btn->setEnabled(false);
//将start_Btn设置成可用状态
ui->start_Btn->setEnabled(true);
ui->clock_lineEdit->setEnabled(true);
//停止计时器
killTimer(tid2);
//将count置零
count = 0;
}
widget.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>613</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QLabel" name="time_lab">
<property name="geometry">
<rect>
<x>50</x>
<y>60</y>
<width>321</width>
<height>161</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>22</pointsize>
<weight>75</weight>
<italic>false</italic>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(85, 255, 255);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLineEdit" name="clock_lineEdit">
<property name="geometry">
<rect>
<x>430</x>
<y>60</y>
<width>291</width>
<height>71</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 0);</string>
</property>
</widget>
<widget class="QPushButton" name="start_Btn">
<property name="geometry">
<rect>
<x>430</x>
<y>160</y>
<width>121</width>
<height>61</height>
</rect>
</property>
<property name="text">
<string>开始</string>
</property>
</widget>
<widget class="QPushButton" name="stop_Btn">
<property name="geometry">
<rect>
<x>590</x>
<y>160</y>
<width>121</width>
<height>61</height>
</rect>
</property>
<property name="text">
<string>停止</string>
</property>
</widget>
<widget class="QTextEdit" name="read_textEdit">
<property name="geometry">
<rect>
<x>50</x>
<y>250</y>
<width>671</width>
<height>341</height>
</rect>
</property>
<property name="html">
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:18pt; color:#55ffff;">故人西辞黄鹤楼,唱跳Rap打篮球。春风又绿江南岸,练习长达两年半。清明时节雨坤坤,路上行人梳中分,借问背带何处有,牧童遥指我爱坤。长江后浪推前浪,爱坤啥样我啥样。鸡冠头背带裤,我是爱坤你记住。向阳花木易为春,听说你爱蔡徐坤。小黑子树枝666,蒸虾头,好丸吗?树枝赶人,蒸五鱼,食不食油饼,我家鸽鸽在胎上拿姜拿到手软,他那么努力,这样叫我怎么荔枝?已经橘爆你了,再这样我就抱紧了,劝你苏珊,不然我们爱坤就紫砂了。</span></p></body></html></string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
思维导图: