24/01/11 qt work

  1. 作业:
cpp 复制代码
xx.h 文件


#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QDate>
#include <QTime>
#include <QTimerEvent>
#include <QMessageBox>
#include <QDebug>
#include <QTextToSpeech>

QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();
    //重写定时器事件
    void timerEvent(QTimerEvent *event);

private slots:
    void on_pushButton_clicked();

private:
    Ui::MyWidget *ui;
    int id;
    int id2;
    QString time;
    QString time2;
    //创建一个语言播报者
    QTextToSpeech *speecher;
};
#endif // MYWIDGET_H


#主程序

#include "mywidget.h"
#include "ui_mywidget.h"

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

    this->setWindowFlag(Qt::FramelessWindowHint);
    //去掉空白部分
    this->setAttribute(Qt::WA_TranslucentBackground);

    //启动定时器
    id = startTimer(1000);
    //给语音播报者实例化空间
    speecher = new QTextToSpeech(this);
}

MyWidget::~MyWidget()
{
    delete ui;
    killTimer(id);
    killTimer(id2);
}

void MyWidget::timerEvent(QTimerEvent *event)
{
    //判断定时器类型
    if(event->timerId() == id){

        QDate date = QDate::currentDate();
        //得到当前时间
        QString str1 = date.toString("yyyy-MM-dd");
        QTime time =  QTime::currentTime();

        QString str = time.toString("HH:mm:ss");
        time2 = str1+" "+str;
        ui->timeLabel->setText(time2);
        ui->timeLabel->setAlignment(Qt::AlignCenter);
    }else if(event->timerId() == id2){
        //时间一致,进行播报
        if(time == time2){
            for(int i=0;i<5;i++){
                speecher->say(ui->messageLabel->text());
                _sleep(200);
            }
        }
    }
}

//设置闹钟时间
void MyWidget::on_pushButton_clicked()
{
    QString str = ui->timeEdit->text();
    if(str == NULL){
        QMessageBox::information(this,
                                 "提示",
                                 "时间框不能为空");

    }

    time = str;
    qDebug() << time;

    //启动定时器
    id2 = startTimer(1000);
}
  1. 思维导图
相关推荐
CC__xy4 分钟前
04 类型别名type + 检测数据类型(typeof+instanceof) + 空安全+剩余和展开(运算符 ...)简单类型和复杂类型 + 模块化
开发语言·javascript·harmonyos·鸿蒙
萤丰信息15 分钟前
技术赋能安全:智慧工地构建城市建设新防线
java·大数据·开发语言·人工智能·智慧城市·智慧工地
Pocker_Spades_A37 分钟前
飞算JavaAI家庭记账系统:从收支记录到财务分析的全流程管理方案
java·开发语言
CHEN5_022 小时前
【Java基础常见辨析】重载与重写,深拷贝与浅拷贝,抽象类与普通类
java·开发语言
Despacito0o2 小时前
C语言基础:变量与进制详解
java·c语言·开发语言
nightunderblackcat2 小时前
进阶向:人物关系三元组,解锁人物关系网络的钥匙
开发语言·python·开源·php
科大饭桶4 小时前
C++入门自学Day11-- String, Vector, List 复习
c语言·开发语言·数据结构·c++·容器
范范之交4 小时前
JavaScript基础语法two
开发语言·前端·javascript
Felven4 小时前
C. Game of Mathletes
c语言·开发语言
点云SLAM4 小时前
C++中内存池(Memory Pool)详解和完整示例
开发语言·c++·内存管理·内存池·new/delete·malloc/free