Qt day5

思维导图

Widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTime>
#include<QTimerEvent>
#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)override;

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    int id;
    QTextToSpeech *speecher;
    bool ok=false;
};
#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"
#include<QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , speecher(new QTextToSpeech(this))//初始化语音播报对象
{
    ui->setupUi(this);
    id=startTimer(1000);//开启定时器

}

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

void Widget::on_pushButton_clicked()
{
    //按钮按下判断按钮上的文本
    if(ui->pushButton->text()=="启动")
    {
       //当按钮上文本为启动时将闹钟启动
        ui->pushButton->setText("关闭");
        ok=true;
    }
    else
    {
        //当按钮上文本为关闭时说明闹钟已经启动,关闭闹钟
        ui->pushButton->setText("启动");
        ok=false;
    }
}
void Widget::timerEvent(QTimerEvent *e)
{
     if(e->timerId()==id)
     {
         QTime sys_time=QTime::currentTime();//获取当前系统事件
         QString t=sys_time.toString("hh:mm:ss");//将系统时间转换为字符串,设置到label中
         ui->label->setText(t);
         ui->label->setAlignment(Qt::AlignCenter);//设置label的文本居中
         if(t==ui->lineEdit->text()&&ok)//当闹钟时间与系统时间相同并且闹钟开启时开始语音播报label_3中的内容
         {
             for(int i=0;i<5;i++)
             {
                speecher->say(ui->label_3->text());
             }
             ui->pushButton->setText("开启");//关闭闹钟
         }
     }

}
相关推荐
Hello_WOAIAI20 分钟前
2.4 python装饰器在 Web 框架和测试中的实战应用
开发语言·前端·python
搬山.摧城27 分钟前
线程池和单例模式
开发语言·单例模式
百锦再30 分钟前
第1章 Rust语言概述
java·开发语言·人工智能·python·rust·go·1024程序员节
一叶之秋141241 分钟前
QT背景介绍与环境搭建
开发语言·qt
java1234_小锋1 小时前
PyTorch2 Python深度学习 - 模型保存与加载
开发语言·python·深度学习·pytorch2
ACP广源盛139246256731 小时前
(ACP广源盛)GSV2231---DisplayPort 1.4 MST 到 HDMI 2.0/DP/Type-C 转换器(带嵌入式 MCU)
c语言·开发语言·单片机·嵌入式硬件·音视频·mst
quant_19861 小时前
【教程】使用加密货币行情接口 - 查询比特币实时价格
开发语言·后端·python·websocket·网络协议
熊猫_豆豆1 小时前
Python 写一个标准版和程序员版计算器
开发语言·python·计算器
Mr.Jessy2 小时前
Web APIs 学习第四天:DOM事件进阶
开发语言·前端·javascript·学习·ecmascript
QT 小鲜肉2 小时前
【QT/C++】Qt网络编程进阶:UDP通信和HTTP请求的基本原理和实际应用(超详细)
c语言·网络·c++·笔记·qt·http·udp