界面展示:
![](https://img-blog.csdnimg.cn/abdee4baf45649d3809fd5d6c0e99eed.png)
My.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QDebug>
#include <QtTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
signals:
void singal_oneopen();
public:
Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *event); //重写定时器事件
private slots:
void on_clockOnebtn_clicked();
void on_clockTwobtn_clicked();
void on_clockThreebtn_clicked();
void on_AllOpenbtn_clicked();
void on_AllClosebtn_clicked();
private:
Ui::Widget *ui;
int showflag = true;
//小时的列表
QStringList *hour;
//分钟的列表
QStringList *min;
int flag1 = 0; //第一个闹钟是否开启标志
int flag2 = 0; //第二个闹钟是否开启标志
int flag3 = 0; //第三个闹钟是否开启标志
//定义一个播报者
QTextToSpeech speech;
};
#endif // WIDGET_H
main不做改动
My.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//启动定时器
startTimer(1000);
//设置闹钟的小时选择列表
hour = new QStringList;
for(int i=0;i<=23;i++)
{
hour->insert(i,QString("%1").arg(i, 2, 10, QLatin1Char('0')));
}
ui->clockOneHourcomboBox->insertItems(1,*hour);
ui->clockTwoHourcomboBox->insertItems(1,*hour);
ui->clockThreeHourcomboBox->insertItems(1,*hour);
//设置闹钟的分钟选择列表
min = new QStringList;
for(int i=0;i<=59;i++)
{
min->insert(i,QString("%1").arg(i, 2, 10, QLatin1Char('0')));
}
ui->clockOneMincomboBox->insertItems(1,*min);
ui->clockTwoMincomboBox->insertItems(1,*min);
ui->clockThreeMincomboBox->insertItems(1,*min);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *)
{
//获取系统时间
QTime systime = QTime::currentTime();
//将系统时间转换成字符串
QString timeText = systime.toString("hh:mm");
if(showflag)
{
timeText[2] = ':';
showflag = false;
}
else
{
timeText[2] = ' ';
showflag = true;
}
//将时间字符串展示到LCD
ui->timelcd->display(timeText);
int fflag1 = 1;
int fflag2 = 1;
int fflag3 = 1;
//如果1闹钟开启,符合则响起
if(flag1 == 1)
{
if((timeText.startsWith(ui->clockOneHourcomboBox->currentText()) && timeText.endsWith(ui->clockOneMincomboBox->currentText())) )
{
speech.say(ui->OneText->toPlainText());
}
}
//如果2闹钟开启,符合则响起
if(flag2 == 1)
{
if(timeText.startsWith(ui->clockTwoHourcomboBox->currentText()) && timeText.endsWith(ui->clockTwoMincomboBox->currentText()) && fflag2 == 1)
{
fflag2 = 0;
speech.say(ui->TwoText->toPlainText());
fflag2 = 1;
}
}
//如果3闹钟开启,符合则响起
if(flag3 == 1)
{
if(timeText.startsWith(ui->clockThreeHourcomboBox->currentText()) && timeText.endsWith(ui->clockThreeMincomboBox->currentText()) && fflag3 == 1)
{
fflag3 = 0;
speech.say(ui->ThreeText->toPlainText());
fflag3 = 1;
}
}
}
//第一个闹钟开关槽函数
void Widget::on_clockOnebtn_clicked()
{
if(ui->clockOnebtn->text() == "关闭中")
{
//将时间选择设为不可用
ui->clockOneHourcomboBox->setEnabled(false);
ui->clockOneMincomboBox->setEnabled(false);
//将开启关闭按钮设置为开启中
ui->clockOnebtn->setText("开启中");
flag1 = 1;
}
else
{
//将时间选择设为可用
ui->clockOneHourcomboBox->setEnabled(true);
ui->clockOneMincomboBox->setEnabled(true);
//将开启关闭按钮设置为关闭中
ui->clockOnebtn->setText("关闭中");
flag1 = 0;
}
}
//第二个闹钟开关槽函数
void Widget::on_clockTwobtn_clicked()
{
if(ui->clockTwobtn->text() == "关闭中")
{
//将时间选择设为不可用
ui->clockTwoHourcomboBox->setEnabled(false);
ui->clockTwoMincomboBox->setEnabled(false);
//将开启关闭按钮设置为开启中
ui->clockTwobtn->setText("开启中");
flag2 = 1;
}
else
{
//将时间选择设为可用
ui->clockTwoHourcomboBox->setEnabled(true);
ui->clockTwoMincomboBox->setEnabled(true);
//将开启关闭按钮设置为关闭中
ui->clockTwobtn->setText("关闭中");
flag2 = 0;
}
}
//第三个闹钟开关槽函数
void Widget::on_clockThreebtn_clicked()
{
if(ui->clockThreebtn->text() == "关闭中")
{
//将时间选择设为不可用
ui->clockThreeHourcomboBox->setEnabled(false);
ui->clockThreeMincomboBox->setEnabled(false);
//将开启关闭按钮设置为开启中
ui->clockThreebtn->setText("开启中");
flag3 = 1;
}
else
{
//将时间选择设为可用
ui->clockThreeHourcomboBox->setEnabled(true);
ui->clockThreeMincomboBox->setEnabled(true);
//将开启关闭按钮设置为关闭中
ui->clockThreebtn->setText("关闭中");
flag3 = 0;
}
}
//闹钟全部开启槽函数
void Widget::on_AllOpenbtn_clicked()
{
//将全部闹钟设置开启,
ui->clockOneHourcomboBox->setEnabled(false);
ui->clockOneMincomboBox->setEnabled(false);
ui->clockOnebtn->setText("开启中");
flag1 = 1;
ui->clockTwoHourcomboBox->setEnabled(false);
ui->clockTwoMincomboBox->setEnabled(false);
ui->clockTwobtn->setText("开启中");
flag2 = 1;
ui->clockThreeHourcomboBox->setEnabled(false);
ui->clockThreeMincomboBox->setEnabled(false);
ui->clockThreebtn->setText("开启中");
flag3 = 1;
}
//闹钟全部关闭槽函数
void Widget::on_AllClosebtn_clicked()
{
ui->clockOneHourcomboBox->setEnabled(true);
ui->clockOneMincomboBox->setEnabled(true);
ui->clockOnebtn->setText("关闭中");
flag1 = 0;
ui->clockTwoHourcomboBox->setEnabled(true);
ui->clockTwoMincomboBox->setEnabled(true);
ui->clockTwobtn->setText("关闭中");
flag2 = 0;
ui->clockThreeHourcomboBox->setEnabled(true);
ui->clockThreeMincomboBox->setEnabled(true);
ui->clockThreebtn->setText("关闭中");
flag3 = 0;
}