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. 思维导图
相关推荐
上线之叁几秒前
小迪安全-tp框架反序列化,利用链,rce执行,文件删除
java·开发语言
躺着听Jay2 分钟前
QCustomPlot-相关优化
java·qt·算法
新知图书23 分钟前
第一个Qt开发的OpenCV程序
开发语言·qt
梦里藍天33 分钟前
QT 调用动态链接库
qt
阿斌_bingyu7091 小时前
Arduino开发物联网ESP32快速入门指南(包含开发语言说明、学习路径和实战教程)
开发语言·物联网·学习
这个懒人2 小时前
linux下io操作详细解析
开发语言·c++·io
东方醴歌2 小时前
VMware安装飞牛私有云fnOS并挂载小雅Alist实现异地远程访问
开发语言·后端·golang
暗影~行星2 小时前
C语言,原码、补码、反码
c语言·开发语言
晨曦5432102 小时前
python——正则表达式
开发语言·正则表达式
得鹿梦鱼、2 小时前
QT控件 修改QtTreePropertyBrowser自定义属性编辑器源码,添加第一列标题勾选,按钮,右键菜单事件等功能
qt·propertybrowser·属性编辑·treeproperty