做了一个自制的小闹钟,能够自己输入时间,以及对应的闹铃,时间到了自动播放设定的闹铃,可以随时取消重新设定,采用分文件编译
注意:需要在.pro文件中加入:QT += core gui texttospeech
代码如下:
wideget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QKeyEvent> //键盘事件类
#include <QMouseEvent> //鼠标事件类
#include <QIcon>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QMovie>
#include <QObject>
#include <QMessageBox>
#include <QTimer> //定时器类
#include <QTime> //时间类
#include <QTimerEvent> //定时器事件类
#include <QDateTime> //日期时间类
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
signals:
public slots:
void start_slots();
void timeout_slots(); //自定义处理超时信号的槽函数
void cancel_slots(); //自定义取消按钮槽函数
public:
Widget(QWidget *parent = nullptr);
~Widget();
void mouseMoveEvent(QMouseEvent *event); //鼠标移动事件
void mousePressEvent(QMouseEvent *event); //鼠标点击事件
void speakText(const QString &text);
private:
Ui::Widget *ui;
QPoint temp; //移动窗口中间辅助向量
QLabel *system_time,*lab1; //显示系统时间
QLineEdit *mod_time,*clock_txt; //可编辑的时间、闹钟输出的文字
QPushButton *start_button,*cancel_button; //启动按钮和取消按钮
int tid = 0; //定时器id号
QTimer t1; //定义一个定时器变量
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(500,500); //设置页面初始尺寸大小
this->setWindowFlag(Qt::FramelessWindowHint); //将头部隐藏
//标签类
system_time = new QLabel(this); //系统时间
system_time->resize(200,140);
system_time->move(50,50);
system_time->setScaledContents(true);
system_time->setStyleSheet("background-color:pink; border-radius:20;\
font-size:20px;font-weight:bold");
system_time->setWindowOpacity(0.1);
//按钮类
start_button = new QPushButton("启动",this);
cancel_button = new QPushButton("取消",this);
//设置位置
start_button->move(system_time->x()+system_time->width()+15,110);
cancel_button->move(start_button->x()+start_button->width()+5,start_button->y());
//设置大小
start_button->resize(90,80);
cancel_button->resize(90,80);
// start_button->setStyleSheet("border-radius:20");
//行标签类
mod_time = new QLineEdit(this);
mod_time->move(system_time->y()+system_time->width()+15,system_time->y());
mod_time->setPlaceholderText("输入定时");
mod_time->resize(180,30);
clock_txt = new QLineEdit(this);
clock_txt->move(50,230);
clock_txt->resize(400,250);
clock_txt->setPlaceholderText("输入闹铃");
// 设置文本对齐到左上角
clock_txt->setAlignment(Qt::AlignLeft | Qt::AlignTop);
clock_txt->setStyleSheet("QLineEdit { padding-left: 0px; }");
//连接信号与槽,按下按钮开始执行功能
connect(start_button, &QPushButton::clicked, this, &Widget::start_slots);
connect(&t1, &QTimer::timeout, this, &Widget::timeout_slots);
connect(cancel_button, &QPushButton::clicked, this, &Widget::cancel_slots);
}
Widget::~Widget()
{
delete ui;
}
//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos() - temp);
}
//鼠标点击事件
void Widget::mousePressEvent(QMouseEvent *event)
{
temp = event->globalPos() - this->pos(); //求出中间辅助变量
if(event->button() == Qt::RightButton)
{
this->close();
}
}
void Widget::start_slots()
{
t1.start(1000); //每隔指定时间,发送一个timeout信号
}
void Widget::timeout_slots()
{
//获取系统时间
QTime sysTime = QTime::currentTime();
//将QTime类对象转变成字符串
QString sysTimeStr = sysTime.toString("hh:mm:ss");
this->system_time->setText(sysTimeStr);
system_time->setAlignment(Qt::AlignCenter);
//将3个地方设置成不可点击
start_button->setEnabled(false);
mod_time->setReadOnly(true);
clock_txt->setReadOnly(true);
//比较逻辑,如果和我输入的时间相等,就发出声音
QString modTimeStr = mod_time->text();
if(sysTimeStr == modTimeStr)
{
qDebug()<<"发出声音";
speakText(clock_txt->text());
}
}
void Widget::cancel_slots()
{
int res = QMessageBox::information(this,
"提示",
"真的要取消么",
QMessageBox::Yes|QMessageBox::No,
QMessageBox::Yes);
if(res == QMessageBox::Yes)
{
qDebug()<<"解除限制功能";
t1.stop();
start_button->setEnabled(true);
mod_time->setReadOnly(false);
clock_txt->setReadOnly(false);
}
else if(res == QMessageBox::No)
{
qDebug()<<"继续执行程序";
}
}
void Widget::speakText(const QString &text)
{
QTextToSpeech *speaker = new QTextToSpeech;
speaker->say(text);
}