闹钟
.h
#ifndef NAOZHONG_H
#define NAOZHONG_H
#include <QWidget>
#include<QLCDNumber>
#include<QTime>
#include<QMouseEvent>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QTextToSpeech>
#include <QPaintEvent>
#include <QPainter>
QT_BEGIN_NAMESPACE
namespace Ui { class naozhong; }
QT_END_NAMESPACE
class naozhong : public QWidget
{
Q_OBJECT
signals:
void signal1();
public:
naozhong(QWidget *parent = nullptr);
~naozhong();
void timerEvent(QTimerEvent *e); //重写定时器事件
public slots:
void on_btn1();
void on_btn2();
void on_signal1();
void paintEvent(QPaintEvent *event); //重写设置背景图虚函数
private:
Ui::naozhong *ui;
QLCDNumber *colock; //定义一个时钟指针
//定义闪烁秒的状态
int showFlag = 1;
QPushButton *btn1; //开始按钮
QPushButton *btn2; //停止按钮
QLineEdit *edit1; //定义一个可以定时的行编辑器
QTextEdit *edit2; //定义一个可以定时的文本编辑器
int flag1 = 0; //定义一个播报flag
//定义一个播报者
QTextToSpeech speech;
QString timeText; //存储时间
};
#endif // NAOZHONG_H
.cpp
#include "naozhong.h"
#include "ui_naozhong.h"
naozhong::naozhong(QWidget *parent)
: QWidget(parent)
, ui(new Ui::naozhong)
{
ui->setupUi(this);
colock = new QLCDNumber(this);
//美化时钟样子
colock->setFixedSize(500,150);
//调整透明度
colock->setWindowOpacity(0);
//启动定时器
startTimer(1000);
//定时文本
edit1 = new QLineEdit(this); //定时的文本
edit1->setPlaceholderText("请定时"); //设置提示文字
edit1->resize(190,60); //设置大小
//edit1->resize(2*(btn1->width())+20,60); //设置大小
edit1->move(colock->width()+20, 0); //移动位置
edit1->setFont(QFont("", 28 , QFont::Bold));
edit1->setWindowOpacity(0); //将透明度设置为0
edit1->setStyleSheet("background:transparent"); //将透明度设置为0
//启动按钮
btn1 = new QPushButton("START", this);
btn1->resize(85,45); //设置大小
btn1->move(edit1->x(), edit1->height()+30); //移动位置
btn1->setFlat(true); // 设置按钮透明
btn1->setFont(QFont("黑体", 20));
//停止按钮
btn2 = new QPushButton("STOP", this);
btn2->resize(85,45); //设置大小
btn2->move(btn1->x()+btn1->width()+20, btn1->y()); //移动位置
btn2->setFlat(true); // 设置按钮透明
btn2->setFont(QFont("黑体", 20));
//备注文本
edit2 = new QTextEdit(this);
edit2->setPlaceholderText("请输入备忘录"); //设置提示文字
edit2->resize(colock->width()+20+edit1->width(),60); //设置大小
edit2->move(0, colock->height()+20); //移动位置
edit2->setFont(QFont("", 20 , QFont::Bold));
edit2->setWindowOpacity(0); //将透明度设置为0
edit2->setStyleSheet("background:transparent"); //将透明度设置为0
this->setFixedSize(colock->width()+30+edit1->width(), colock->height()+20+edit2->height()); //设置主控件的大小
this->setWindowTitle("奇奇牌闹钟");
//去掉边框
//this->setWindowFlags(Qt::FramelessWindowHint);
//设置两个按钮的槽函数
connect(btn1, &QPushButton::clicked, this, &naozhong::on_btn1);
connect(btn2, &QPushButton::clicked, this, &naozhong::on_btn2);
//设置播报槽函数
connect(this, &naozhong::signal1, this, &naozhong::on_signal1);
}
naozhong::~naozhong()
{
delete ui;
}
void naozhong::timerEvent(QTimerEvent *)
{
//1、获取系统事件
QTime sysTime = QTime::currentTime();
//2、将系统事件转换为字符串
timeText = sysTime.toString("hh:mm");
if(showFlag)
{
timeText[2] = ':';
showFlag = 0;
}else {
timeText[2] = ' ';
showFlag = 1;
}
//将时间字符串展示到LCD中
colock->display(timeText);
if(edit1->text() == timeText)
{
emit signal1(); //发播报信号
}
}
void naozhong::on_btn1()
{
edit1->setReadOnly(true); //将定时文本设置为不可编辑
edit2->setReadOnly(true); //将备注文本设置为不可编辑
}
void naozhong::on_btn2()
{
flag1 = 1;
edit1->clear(); //将定时文本清空
edit2->clear(); //将备忘录文本清空
edit1->setReadOnly(false); //将定时文本设置为可编辑
edit2->setReadOnly(false); //将备注文本设置为可编辑
}
void naozhong::on_signal1()
{
if(0 == flag1)
speech.say(edit2->toPlainText());
}
void naozhong::paintEvent(QPaintEvent *) //设置背景图
{
QPainter *painter = new QPainter(this);
QPixmap pic = QPixmap(":/new/prefix1/1111.png");
painter->drawPixmap(0,0,this->width(),this->height(),pic);
}