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);
    }
}
相关推荐
guest_8815 分钟前
用 Python 打造打篮球字符动画!控制台彩色炫酷输出,抖音搞怪视频灵感还原
开发语言·python·numpy·pillow
互联网打工人no118 分钟前
.NET8 依赖注入组件
开发语言·c#·.net·ioc
道剑剑非道24 分钟前
QT开发技术【QT实现桌面右下角消息】
开发语言·数据库·qt
fs哆哆25 分钟前
在VB.net和VBA中,自定义函数GetTargetSheet()返回工作表对象
java·开发语言·前端·javascript·ecmascript
努力学习的小廉28 分钟前
我爱学算法之—— 二分查找(上)
开发语言·c++·算法
东木月30 分钟前
Python解析地址中省市区街道
开发语言·python
此刻我在家里喂猪呢2 小时前
qt之开发大恒usb3.0相机一
qt·相机开发·大恒usb3.0相机·工业相机开发
weixin_307779132 小时前
PySpark实现ABC_manage_channel逻辑
开发语言·python·spark
??? Meggie3 小时前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
酷爱码5 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python