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. 思维导图
相关推荐
крон2 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan3 小时前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊3 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1183 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之4 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?4 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头4 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
liuyang-neu5 小时前
java内存模型JMM
java·开发语言
我很好我还能学6 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
蓝婷儿7 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习