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);
    }
}
相关推荐
Felix_One4 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit7 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理12 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12312 天前
matlab画图工具
开发语言·matlab
dustcell.12 天前
haproxy七层代理
java·开发语言·前端
norlan_jame12 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone12 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ40220549612 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月12 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_5312371712 天前
C语言-数组练习进阶
c语言·开发语言·算法