基于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

头文件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTimer>
#include <QTextToSpeech>
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 *e);

private slots:
    void on_pushButton_clicked();

    void on_startButton_clicked();

private:
    Ui::Widget *ui;
    int timeid=startTimer(1000);
    int alarmid;
    QTextToSpeech *speecher;
};
#endif // WIDGET_H

主函数

功能函数

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , speecher(new QTextToSpeech) // 创建文本转语音对象
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint); // 设置窗口无边框
    ui->eventlabel->setAlignment(Qt::AlignCenter); // 设置事件标签居中对齐
    ui->alarmlabel->setAlignment(Qt::AlignCenter); // 设置闹钟标签居中对齐

}


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

// 重写时钟事件函数
void Widget::timerEvent(QTimerEvent *e)
{
    // 检查是否为时间更新定时器
    if(e->timerId()==timeid){
        QTime sys_time = QTime::currentTime(); // 获取当前系统时间
        QString t = sys_time.toString("hh:mm:ss"); // 格式化时间为字符串
        ui->timelabel->setText(t); // 更新时间标签的文本
        ui->timelabel->setAlignment(Qt::AlignCenter); 
    }
    // 检查是否为闹钟定时器
    if(e->timerId()==alarmid){
        QTime sys_time = QTime::currentTime(); // 获取当前系统时间
        QString t = sys_time.toString("hh:mm:ss"); // 格式化时间为字符串
        // 检查是否到达设定的闹钟时间
        if(ui->settmlabel->text()==t){
            // 循环播放闹钟提示语音
            for (int i=0;i<5;i++) {
                speecher->say(ui->alarmlabel->text()); // 使用文本转语音播放闹钟标签的文本
            }
        }
    }
}

// 关闭按钮的点击事件处理函数
void Widget::on_pushButton_clicked()
{
    this->close(); // 关闭窗口
}

// 启动/关闭按钮的点击事件处理函数
void Widget::on_startButton_clicked()
{
    // 检查按钮文本,判断当前是启动还是关闭状态
    if(ui->startButton->text()=="启动"){
        ui->startButton->setText("关闭"); // 设置按钮文本为"关闭"
        alarmid=startTimer(1000); // 启动闹钟定时器,每秒触发一次
    }else{
        ui->startButton->setText("启动"); // 设置按钮文本为"启动"
        killTimer(alarmid); // 停止闹钟定时器
    }
}

实现效果

知识点思维导图

相关推荐
枫叶丹46 分钟前
【Qt开发】多元素类控件(二)-> QTableWidget
开发语言·qt
bin91538 分钟前
当AI开始‘映射‘用户数据:初级Python开发者的创意‘高阶函数‘如何避免被‘化简‘?—— 老码农的函数式幽默
开发语言·人工智能·python·工具·ai工具
怀揣小梦想29 分钟前
跟着Carl学算法--哈希表
数据结构·c++·笔记·算法·哈希算法·散列表
Nebula_g30 分钟前
Java哈希表入门详解(Hash)
java·开发语言·学习·算法·哈希算法·初学者
努力努力再努力wz32 分钟前
【C++进阶系列】:万字详解unordered_set和unordered_map,带你手搓一个哈希表!(附模拟实现unordered_set和unordered_map的源码)
java·linux·开发语言·数据结构·数据库·c++·散列表
加油=^_^=33 分钟前
【C++】哈希表
数据结构·c++·散列表
对纯音乐情有独钟的阿甘34 分钟前
【C++庖丁解牛】哈希表/散列表的设计原理 | 哈希函数
c++·哈希算法·散列表
励志不掉头发的内向程序员35 分钟前
【STL库】哈希表的原理 | 哈希表模拟实现
开发语言·c++·学习·散列表
玩镜的码农小师兄36 分钟前
[从零开始面试算法] (04/100) LeetCode 136. 只出现一次的数字:哈希表与位运算的巅峰对决
c++·算法·leetcode·面试·位运算·hot100