QTDAY3

闹钟

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent>  //定时器事件处理函数
#include <QTime>        //时间类
#include <QString>
#include <QPushButton>
#include <QTextToSpeech>
#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 slots:
    void on_startbtn_clicked();

    void on_stopbtn_clicked();

private:
    Ui::Widget *ui;

    int tid;
    QTextToSpeech *speecher;

};
#endif // WIDGET_H

源文件

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

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

    //启动定时器
    tid = startTimer(1000);

    //让停止按钮默认不可用
    ui->stopbtn->setEnabled(false);

    //构造演讲者
    speecher = new QTextToSpeech(this);

    //窗口标题
    this->setWindowTitle("clock");
    this->setWindowIcon(QIcon(":/C:/Users/PC/Desktop/my/my_icon.png"));

    //设置透明度
    this->setWindowOpacity(0.9);

}

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


void Widget::on_startbtn_clicked()
{
    //将启动按钮,两个编辑器不可用,停止按钮可用
    ui->stopbtn->setEnabled(true);
    ui->startbtn->setEnabled(false);
    ui->clocktxtedit->setEnabled(false);
    ui->clocktimeedit->setEnabled(false);
}

void Widget::on_stopbtn_clicked()
{
    int res = QMessageBox::warning(this,
                                   "警告",
                                   "确定停止吗",
                                   QMessageBox::Yes | QMessageBox::No);

    //将启动按钮,两个编辑器可用,停止按钮不可用
    if(res == QMessageBox::Yes)
    {
        ui->stopbtn->setEnabled(false);
        ui->startbtn->setEnabled(true);
        ui->clocktxtedit->setEnabled(true);
        ui->clocktimeedit->setEnabled(true);
        ui->clocktimeedit->clear();
    }
}

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == tid)
    {
        //获取系统时间
        QTime sys_time = QTime::currentTime();

        //将时间转换为字符串
        QString t = sys_time.toString("hh:mm:ss");

        //将字符串展示到ui界面
        ui->systimelab->setText(t);
        ui->systimelab->setAlignment(Qt::AlignCenter);

        //判断系统时间是否和设定时间相同
        if(ui->systimelab->text() == ui->clocktimeedit->text())
        {
            speecher->say(ui->clocktxtedit->toPlainText());
        }
    }
}

思维导图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值