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);
    }
}
相关推荐
橘颂TA几秒前
线程池与线程安全:后端开发的 “性能 + 安全” 双维实践
java·开发语言·安全
bruce_哈哈哈5 分钟前
go语言初认识
开发语言·后端·golang
Summer_Uncle8 分钟前
【QT学习】qt项目使用MySQL数据库
数据库·qt·学习
Henry Zhu12317 分钟前
Qt样式系统详解(上)
qt
十五年专注C++开发19 分钟前
VS2019编译的C++程序,在win10正常运行,在win7上Debug正常运行,Release运行报错0xC0000005,进不了main函数
开发语言·c++·报错c0x0000005
一条咸鱼_SaltyFish19 分钟前
[Day13] 微服务架构下的共享基础库设计:contract-common 模块实践
开发语言·人工智能·微服务·云原生·架构·ai编程
隐退山林21 分钟前
JavaEE:多线程初阶(一)
java·开发语言·jvm
C_心欲无痕25 分钟前
ts - 模板字面量类型与 `keyof` 的魔法组合:`keyof T & `on${string}`使用
linux·运维·开发语言·前端·ubuntu·typescript
最贪吃的虎27 分钟前
Redis其实并不是线程安全的
java·开发语言·数据库·redis·后端·缓存·lua