2023.06.15 QT day4

基于QT的简易闹钟 

头文件widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>              //定时器类的头文件
#include <QTime>               //时间类的头文件
#include <QTimerEvent>         //定时器事件处理类
#include <QDateTime>           //日期时间类
#include <QMessageBox>         //消息提醒框类
#include <QTextToSpeech>       //文本转语音

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_start_btn_clicked();
    void on_stop_btn_clicked();
    void timeout_slot();

private:
    Ui::Widget *ui;

    QTimer *t1;     //定义一个定时器指针
    bool flag=0;    //判断闹钟是否启动
    QTextToSpeech *speecher;       //定义播报者指针
};

#endif // WIDGET_H

功能函数widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(640,480);
    this->setWindowTitle("闹钟 - by zzy");

    //实例化定时器指针
    t1 = new QTimer(this);

    //将t1的timeout信号绑定到自定义的槽函数中
    connect(t1, &QTimer::timeout, this, &Widget::timeout_slot);
    t1->start(200);

    //实例化播报者对象
    speecher = new QTextToSpeech(this);

    //设置关闭按钮不可用
    ui->stop_btn->setEnabled(false);

    //设置label显示格式
    ui->label->setAlignment(Qt::AlignCenter);
}

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

//启动按钮的槽函数
void Widget::on_start_btn_clicked()
{
    //设置按钮/文本框是否可用
    ui->start_btn->setEnabled(false);
    ui->stop_btn->setEnabled(true);
    ui->timeEdit->setEnabled(false);
    ui->textEdit->setEnabled(false);
    flag = 1;
}

//关闭按钮的槽函数
void Widget::on_stop_btn_clicked()
{
    //设置按钮/文本框是否可用
    ui->start_btn->setEnabled(true);
    ui->stop_btn->setEnabled(false);
    ui->timeEdit->setEnabled(true);
    ui->textEdit->setEnabled(true);
    flag = 0;
}

//定时器时间结束的槽函数
void Widget::timeout_slot()
{
    //获取系统当前时间
    QTime sys_time = QTime::currentTime();
    //将QTime转换成字符串
    QString time = sys_time.toString("hh:mm:ss");
    QString time2 = ui->timeEdit->time().toString("hh:mm:ss");
    //将时间显示在label上
    ui->label->setText(time);

    if(flag==1 && time == time2)
    {
        speecher->say(ui->textEdit->toPlainText());
        QMessageBox::information(this, "闹钟", ui->textEdit->toPlainText());
        ui->start_btn->setEnabled(true);
        ui->stop_btn->setEnabled(false);
        ui->timeEdit->setEnabled(true);
        ui->textEdit->setEnabled(true);
        flag = 0;
    }
}

运行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值