用Qt自制一个小闹钟

小闹钟

功能

当按下启动按钮时,停止按钮可用,启动按钮不可用,闹钟无法设置,无法输入自定义内容

当按下停止按钮时,暂停播报,启动按钮可用,闹钟可以设置,可以输入自定义内容

.pro文件

cpp 复制代码
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

RESOURCES += \
    Icon.qrc

widget.h文件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>      //定时器类
#include <QTime>       //时间类
#include <QTimerEvent>   //定时器事件类的头文件
#include <QDateTime>     //日期时间类
#include <QDateTimeEdit>
#include <QDebug>
#include <QTextToSpeech>    //朗读
#include <QTextEdit>
#include <QMetaObject>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    //重写定时器事件处理函数
    void timerEvent(QTimerEvent *event)override;



signals:
    void my_signal();
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();


private:
    Ui::Widget *ui;
    //定义一个定时器的id
    int timer_id;        //基于事件处理函数的定时器
    int timer_id1;
    QTextToSpeech *speech;
    int i = 0;
    int flag = 0;
    QString text;
    QDateTime sys_dt;


};
#endif // WIDGET_H

main.cpp文件

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文件

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("小闹钟");
    timer_id = this->startTimer(5);
    ui->pushButton_2->setEnabled(false);
    ui->textEdit->setPlaceholderText("请输入闹钟响时播报的内容");
    this->setWindowIcon(QIcon(":/new/prefix1/666.png"));

}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId()) //== timer_id)  //用来判断不同的定时器的id
    {
        //获取当前系统的日期时间
        sys_dt = QDateTime::currentDateTime();
        //展示时间到ui界面的lable2中
        ui->label->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
        //居中显示               标签文本对齐方式
        ui->label->setAlignment(Qt::AlignCenter);

        ui->label->setFont(QFont("微软雅黑",20));

        QString timeText = sys_dt.toString("yyyy-MM-dd hh:mm:ss");
        QString timeText1 = ui->dateTimeEdit->text();
        if(flag == 1)
        {

            if(timeText1 == timeText)
            {
                speech->say(text); // 朗读文本

            }
        }
    }
}
void Widget::on_pushButton_clicked()
{
    flag = 1;
    speech = new QTextToSpeech;
    text = ui->textEdit->toPlainText();
    ui->pushButton_2->setEnabled(true);
    ui->pushButton->setEnabled(false);
    ui->textEdit->setEnabled(false);
    ui->dateTimeEdit->setEnabled(false);
}

void Widget::on_pushButton_2_clicked()
{
    flag = 0;
    ui->pushButton->setEnabled(true);
    ui->pushButton_2->setEnabled(false);
    ui->textEdit->setEnabled(true);
    ui->dateTimeEdit->setEnabled(true);
    speech->stop();
}

widget.ui文件

相关推荐
杜子不疼.9 分钟前
《Python学习之第三方库:开启无限可能》
开发语言·python·学习
青川入梦28 分钟前
MyBatis极速通关上篇:Spring Boot环境搭建+用户管理实战
java·开发语言·mybatis
CC__xy1 小时前
04 类型别名type + 检测数据类型(typeof+instanceof) + 空安全+剩余和展开(运算符 ...)简单类型和复杂类型 + 模块化
开发语言·javascript·harmonyos·鸿蒙
萤丰信息1 小时前
技术赋能安全:智慧工地构建城市建设新防线
java·大数据·开发语言·人工智能·智慧城市·智慧工地
Pocker_Spades_A1 小时前
飞算JavaAI家庭记账系统:从收支记录到财务分析的全流程管理方案
java·开发语言
CHEN5_023 小时前
【Java基础常见辨析】重载与重写,深拷贝与浅拷贝,抽象类与普通类
java·开发语言
Despacito0o3 小时前
C语言基础:变量与进制详解
java·c语言·开发语言
nightunderblackcat3 小时前
进阶向:人物关系三元组,解锁人物关系网络的钥匙
开发语言·python·开源·php
科大饭桶4 小时前
C++入门自学Day11-- String, Vector, List 复习
c语言·开发语言·数据结构·c++·容器
范范之交4 小时前
JavaScript基础语法two
开发语言·前端·javascript