QTday3消息弹框/计时器

闹钟小软件

 widget.cpp

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
#include <QPushButton>
#include <QLabel>
#include <QTimer>
#include <QTimerEvent>
#include <QTime>
#include <QMessageBox>

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:
    Ui::Widget *ui;
public:
    int timeId;
    bool isSettingAlarmClock = false;
private slots:
    void on_startBtn_clicked();
    void on_stopBtn_clicked();
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->stopBtn->setEnabled(false);
    ui->startBtn->setEnabled(true);
    ui->clockLineEdit->setEnabled(true);
    ui->textEdit->setEnabled(false);
    ui->textEdit->clear();
    isSettingAlarmClock = false;
    //开启定时器
    timeId = startTimer(1000);
}

void Widget::timerEvent(QTimerEvent *e){
    //如果定时器不是 timeId, 直接返回
    if(e->timerId() != timeId){
        return;
    }
    //刷新展示当前时间
    QTime sys_time = QTime::currentTime();
    QString time = sys_time.toString("hh:mm:ss");
    ui->currentTimeBtn->setText(time);
    //如果 设置闹钟标识为false 直接返回
    if(true !=isSettingAlarmClock){
        return;
    }
    //设置闹钟标识为true 判断是否到设置的闹钟时间, 是->播报声音或提示, 虚拟机播放不了声音,就只提示
    if(time == ui->clockLineEdit->text()){
        int ret = QMessageBox::information(this,"提示","闹钟时间到啦,赶紧起来电脑开机学习!!!",QMessageBox::Yes,QMessageBox::Yes);
        if(ret == QMessageBox::Yes){
            qDebug()<<"好的,我赶紧起来开电脑";
        }
        qDebug()<<"闹钟时间到了";
    }
}

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

void Widget::on_startBtn_clicked()
{
    if(ui->clockLineEdit->text().isEmpty() || ui->clockLineEdit->text().isNull()){
        int ret = QMessageBox::information(this,"提示","还没设置闹钟时间,来设置时间:比如:12:00:00",QMessageBox::Yes,QMessageBox::Yes);
        if(ret == QMessageBox::Yes){
            qDebug()<<"好的,我赶紧起来开电脑";
        }
        return;
    }
    ui->startBtn->setEnabled(false);
    ui->stopBtn->setEnabled(true);
    ui->clockLineEdit->setEnabled(false);
    ui->textEdit->setEnabled(false);
    ui->textEdit->setText("三更灯火五更鸡\n正式男儿读书时\n黑发不知勤学早\n白发方悔读书迟");
    this->isSettingAlarmClock = true;
}

void Widget::on_stopBtn_clicked()
{
    ui->stopBtn->setEnabled(false);
    ui->startBtn->setEnabled(true);
    ui->clockLineEdit->setEnabled(true);
    ui->textEdit->clear();
    ui->textEdit->setEnabled(false);
    ui->clockLineEdit->clear();
    isSettingAlarmClock = false;
}

/*
    程序启动: 停止按钮不可点击/开始按钮可点击/闹钟提示置空/闹钟edit置空/闹钟标识置为false
    1开启定时器tId_currentTime, 记时事件处理显示当前时间
    2点击开始按钮: 停止按钮可点击/开始按钮不可点击/闹钟提示文本设置/闹钟edit不可点击/闹钟标识置为true. 比较当前时间 == 编辑时间, 相同播报文字 否则不播报,
    3点击停止: 闹钟标识置为false 停止按钮不可点击/开始按钮可点击/闹钟提示置空/闹钟edit置空. 定时器不比较时间 只展示时间
*/

markdown笔记:思维导图不利于复习知识点和查找搜索, 所以写的markdown

猜你喜欢

转载自blog.csdn.net/sunmumu122212222/article/details/131969019