2011-11-17自制闹钟

使用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;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值