3.26 day5 QT

头文件:

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent> //时间事件
#include <QTime> //时间类
#include <QTextToSpeech> //文本转语音

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

    void timerEvent(QTimerEvent *e);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    int id,id2;
    QString s;
    QTextToSpeech *speech;
};

#endif // WIDGET_H

源文件:

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    speech(new QTextToSpeech (this))
{
    ui->setupUi(this);
    QTime sys_time = QTime::currentTime();//获取当前系统时间
    s = sys_time.toString("hh:mm:ss");
    ui->systimelabel->setText(s);
    ui->systimelabel->setAlignment(Qt::AlignCenter);//居中显示
    id = startTimer(1000);
}

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

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == id)
    {
        QTime sys_time = QTime::currentTime();
        s = sys_time.toString("hh:mm:ss");//获取当前系统时间
        ui->systimelabel->setText(s);
        ui->systimelabel->setAlignment(Qt::AlignCenter);
    }
    else if(e->timerId() == id2)
    {
        if(ui->systimelabel->text() == ui->lineEdit->text())
        {
            for (int i = 0; i < 5; i++)
            {
                ui->label->setText("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
                speech->say("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
            }
        }
    }
}

void Widget::on_pushButton_clicked()
{
    if(ui->pushButton->text() == "启动")
    {
        id2 = startTimer(1000);
        ui->pushButton->setText("关闭");

    }else {
        ui->pushButton->setText("启动");
        killTimer(id2);
    }
}
相关推荐
ascarl201018 分钟前
准确--k8s cgroup问题排查
java·开发语言
fpcc1 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
莱茵菜苗1 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
爱学习的小道长1 小时前
Python 构建法律DeepSeek RAG
开发语言·python
luojiaao2 小时前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器
终焉代码2 小时前
STL解析——list的使用
开发语言·c++
SoFlu软件机器人2 小时前
智能生成完整 Java 后端架构,告别手动编写 ControllerServiceDao
java·开发语言·架构
英英_2 小时前
视频爬虫的Python库
开发语言·python·音视频
猛犸MAMMOTH2 小时前
Python打卡第46天
开发语言·python·机器学习
多多*2 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap