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("开启");//关闭闹钟
         }
     }

}
相关推荐
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054964 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月4 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237174 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian4 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡4 天前
简单工厂模式
开发语言·算法·c#