cppcpp
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>640</width>
<height>480</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>640</width>
<height>480</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>640</width>
<height>480</height>
</size>
</property>
<property name="windowTitle">
<string>闹钟 - by zzy</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>300</width>
<height>120</height>
</rect>
</property>
<property name="font">
<font>
<family>浪漫雅圆</family>
<pointsize>36</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: rgb(85, 170, 255);</string>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QPushButton" name="start_btn">
<property name="geometry">
<rect>
<x>370</x>
<y>90</y>
<width>100</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<family>浪漫雅圆</family>
<pointsize>12</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>启动</string>
</property>
</widget>
<widget class="QPushButton" name="stop_btn">
<property name="geometry">
<rect>
<x>510</x>
<y>90</y>
<width>100</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<family>浪漫雅圆</family>
<pointsize>12</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>关闭</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>20</x>
<y>160</y>
<width>600</width>
<height>300</height>
</rect>
</property>
</widget>
<widget class="QTimeEdit" name="timeEdit">
<property name="geometry">
<rect>
<x>370</x>
<y>20</y>
<width>240</width>
<height>60</height>
</rect>
</property>
<property name="font">
<font>
<family>浪漫雅圆</family>
<pointsize>12</pointsize>
</font>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer> //定时器类的头文件
#include <QTime> //时间类的头文件
#include <QTimerEvent> //定时器事件处理类
#include <QDateTime> //日期时间类
#include <QMessageBox> //消息提醒框类
#include <QTextToSpeech> //文本转语音
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_start_btn_clicked();
void on_stop_btn_clicked();
void timeout_slot();
private:
Ui::Widget *ui;
QTimer *t1; //定义一个定时器指针
bool flag=0; //判断闹钟是否启动
QTextToSpeech *speecher; //定义播报者指针
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(640,480);
this->setWindowTitle("闹钟 - by zzy");
//给定时器指针实例化空间
t1 = new QTimer(this); //显示当前时间
//将t1的timeout信号绑定到自定义的槽函数中
connect(t1, &QTimer::timeout, this, &Widget::timeout_slot);
t1->start(200);
//实例化播报者对象
speecher = new QTextToSpeech(this);
//设置关闭按钮不可用
ui->stop_btn->setEnabled(false);
//设置label显示格式
ui->label->setAlignment(Qt::AlignCenter);
}
Widget::~Widget()
{
delete ui;
}
//启动按钮的槽函数
void Widget::on_start_btn_clicked()
{
//设置按钮/文本框是否可用
ui->start_btn->setEnabled(false);
ui->stop_btn->setEnabled(true);
ui->timeEdit->setEnabled(false);
ui->textEdit->setEnabled(false);
flag = 1;
}
//关闭按钮的槽函数
void Widget::on_stop_btn_clicked()
{
//设置按钮/文本框是否可用
ui->start_btn->setEnabled(true);
ui->stop_btn->setEnabled(false);
ui->timeEdit->setEnabled(true);
ui->textEdit->setEnabled(true);
flag = 0;
}
//定时器时间结束的槽函数
void Widget::timeout_slot()
{
//获取系统当前时间
QTime sys_time = QTime::currentTime();
//将QTime转换成字符串
QString time = sys_time.toString("hh:mm:ss");
QString time2 = ui->timeEdit->time().toString("hh:mm:ss");
//将时间显示在label上
ui->label->setText(time);
if(flag==1 && time == time2)
{
speecher->say(ui->textEdit->toPlainText());
QMessageBox::information(this, "闹钟", ui->textEdit->toPlainText());
ui->start_btn->setEnabled(true);
ui->stop_btn->setEnabled(false);
ui->timeEdit->setEnabled(true);
ui->textEdit->setEnabled(true);
flag = 0;
}
}