使用QT制作闹钟,具有以下功能:
1:显示当前时间
2:显示当前日期
3:添加设置闹钟时间
4:自定义设置设置闹钟提示事件
5:语音播报提示
6:关闭闹钟
7:开启后防误触修改功能
后期功能:
自定义设置字体,
添加多个闹钟时间,
自定义背景颜色,
自定义字体
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
widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPushButton>
#include<QDateTime>
#include<QTimerEvent>
#include<QTimer>
#include<QMouseEvent>
#include<QTextToSpeech>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public slots:
void on_time();
void on_clock();
void on_start_clicked();
void on_end_clicked();
public:
Widget(QWidget *parent = nullptr);
~Widget();
void mousePressEvent(QMouseEvent *event);
private:
Ui::Widget *ui;
QTimer *time,*clock;
QTextToSpeech reader;
QDateTime currenttime;
};
#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"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowTitle("闹钟");//设置标题
setWindowIcon(QIcon("E:/QT/QT/pic/36.png"));//设置图标
ui->frame->setFrameStyle(QFrame::Raised);
//ui->frame->setFrameStyle(QFrame::Sunken);
//ui->frame->setStyleSheet()
clock = new QTimer(this);
clock->start(1000);
time = new QTimer(this);//实例化一个定时器对象
time->start(1000);
connect(time,&QTimer::timeout,this,&Widget::on_time);
ui->LCD->setDigitCount(8);
ui->date->setDigitCount(10);
ui->date->setSegmentStyle(QLCDNumber::Filled);
connect(ui->start,&QPushButton::clicked,this,&Widget::on_start_clicked);
ui->end->setEnabled(false);
ui->speech->setText("你好啊");
ui->speech->setAlignment(Qt::AlignCenter);
QTextToSpeech reader(this);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
}
}
void Widget::on_start_clicked()
{
ui->lcd->display(ui->limit->text());//时间设置到定时的lcd上。
connect(clock,&QTimer::timeout,this,&Widget::on_clock);
ui->start->setEnabled(false);
ui->limit->setEnabled(false);
ui->speech->setReadOnly(true);
ui->end->setEnabled(true);
}
void Widget::on_end_clicked()
{
disconnect(clock,&QTimer::timeout,this,&Widget::on_clock);
reader.stateChanged(QTextToSpeech::Paused);//设置暂停
ui->start->setEnabled(true);
ui->limit->setEnabled(true);
ui->speech->setReadOnly(false);
ui->end->setEnabled(false);
}
void Widget::on_time()
{
currenttime=QDateTime::currentDateTime();
ui->LCD->display(currenttime.toString("HH:mm:ss"));
ui->date->display(currenttime.toString("yyyy/MM/dd"));
}
void Widget::on_clock()
{
qDebug()<<currenttime.toString("HH:mm")<<ui->limit->text();
if(currenttime.toString("HH:mm").compare(ui->limit->text()) == 0)
{
reader.say(ui->speech->toPlainText());
}
}
Widget::~Widget()
{
delete ui;
}