alarm.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 \
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
RESOURCES += \
icon/icon.qrc
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QIcon> //图标类头文件
#include <QPushButton>
#include <QLineEdit> //行编辑器
#include <QTextEdit>
#include <QLabel> //标签类
#include <QTimer> //定时器类头文件
#include <QTime> //系统时间类头文件
#include <QTextToSpeech> //播报者头文件
#include <QDateTime> //日期时间类
#include <QDebug>
#include <QTimerEvent> //定时器事件类
#include <QLCDNumber>
#include <QPainter>
#include <QPaintEvent>
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 *event)override; //重写定时器事件处理函数
void paintEvent(QPaintEvent *event) override;
private slots:
void LCD_Show();
void on_StartButton_clicked();
void on_CloseButton_clicked();
private:
Ui::Widget *ui;
QLCDNumber *DateLcd;
QLineEdit *DateEdit;
QTextEdit *textEdit;
QPushButton *CloseButton;
QPushButton *StartButton;
//定义LCD屏时间显示的定时器
QTimer *lcd_time;
//定义一个bool类型变量:实现:以秒闪烁
bool ShowFlag = true;
//定义文本输入框的播报者
QTextToSpeech *Text_Speech;
//定义闹钟定时器
int clock_time;
//定义重复播报定时器
int Respeech_time;
};
#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);
//设置固定尺寸
this->setFixedSize(540,410);
//更改窗口标题
this->setWindowTitle("小徐的专属闹钟");
//更改窗口图标
this->setWindowIcon(QIcon(":/1.png"));
//给LCD屏定时器实例化对象
lcd_time = new QTimer(this);
//将timeout信号与处理的槽函数连接
connect(lcd_time,&QTimer::timeout,this,&Widget::LCD_Show);
//启动LCD屏定时器
lcd_time->start(1000);
//设置文本框
textEdit = new QTextEdit(this); //无参构造
textEdit->resize(500,250);
textEdit->move(20,150);
textEdit->setStyleSheet("font:30pt");
textEdit->setAlignment(Qt::AlignCenter);
//时间展示
DateLcd = new QLCDNumber(this);
DateLcd->resize(300,120);
DateLcd->move(20,20);
//设置时间输入框
DateEdit = new QLineEdit(this); //无参构造
DateEdit->resize(170,60);
DateEdit->move(350,20);
DateEdit->setStyleSheet("font:15pt");
DateEdit->setAlignment(Qt::AlignCenter);
//设置开始按钮
StartButton = new QPushButton(this); //无参构造
StartButton->resize(80,30);
StartButton->move(350,90);
StartButton->setText("启动");
//设置取消按钮为不可用状态
CloseButton = new QPushButton(this); //无参构造
CloseButton->resize(80,30);
CloseButton->move(440,90);
CloseButton->setText("停止");
CloseButton->setEnabled(false);
//实例化文本输入框的播报者
Text_Speech = new QTextToSpeech(this);
connect(StartButton, &QPushButton::clicked, this, &Widget::on_StartButton_clicked);
connect(CloseButton, &QPushButton::clicked, this, &Widget::on_CloseButton_clicked);
}
//LCD屏显示时间的槽函数
void Widget::LCD_Show()
{
//获取当前系统时间
QTime sys_time = QTime::currentTime();
//将当前系统时间转化为时间字符串
QString string_time = sys_time.toString("hh:mm");
//实现:以秒闪烁
if(ShowFlag)
{
string_time[2] = ':';
ShowFlag = false;
}
else
{
string_time[2] = ' ';
ShowFlag = true;
}
//展示到ui界面
DateLcd->display(string_time);
}
//启动按钮对应的槽函数
void Widget::on_StartButton_clicked()
{
//设置启动按钮为不可用状态
StartButton->setEnabled(false);
//设置取消按钮为可用状态
CloseButton->setEnabled(true);
//设置文本输入框为不可用状态
textEdit->setEnabled(false);
//设置时间输入框为不可用状态
DateEdit->setEnabled(false);
//启动闹钟定时器
clock_time = this->startTimer(1000);
}
//取消按钮对应的槽函数
void Widget::on_CloseButton_clicked()
{
//设置启动按钮为可用状态
StartButton->setEnabled(true);
//设置取消按钮为不可用状态
CloseButton->setEnabled(false);
//设置文本输入框为可用状态
textEdit->setEnabled(true);
//设置时间输入框为可用状态
DateEdit->setEnabled(true);
//关闭闹钟定时器
// this->killTimer(clock_time);
//关闭重复播报定时器
this->killTimer(Respeech_time);
//清空时间数据输入框
DateEdit->clear();
//清空文本数据输入框
textEdit->clear();
//停止语音播报
Text_Speech->stop();
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == clock_time)
{
//获取当前系统时间
QTime sys_time = QTime::currentTime();
//将当前系统时间转化为时间字符串
QString string_time = sys_time.toString("hh:mm");
//与时间输入框中的数据对比
if(string_time == DateEdit->text())
{
//关闭闹钟定时器
this->killTimer(clock_time);
//启动重复播报定时器
Respeech_time = this->startTimer(5000);
//语音播报起床文本内容
Text_Speech->say(textEdit->toPlainText());
}
}
if(event->timerId() == Respeech_time)
{
//每隔5秒语音播报起床文本内容
Text_Speech->say(textEdit->toPlainText());
}
}
void Widget::paintEvent(QPaintEvent * event)
{
QPainter painter(this);
painter.drawPixmap(rect(),QPixmap(":/3.jpg"),QRect());
}
运行前:
运行时:
运行后: