Qt事件机制

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void timerEvent(QTimerEvent *e);
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::Widget *ui;
     int id;
     QTextToSpeech *speecher;
};
#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
   // , speecher(new QTextToSpeech(this))
{
    ui->setupUi(this);
    ui->label_2->setText("请输入要定闹钟的时间");
    ui->label_3->setText("好好学习,天天向上");
}

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

void Widget:: timerEvent(QTimerEvent *e)
{
    if(e->timerId() == id)
    {
        //获取系统时间
        QTime sys_time =QTime::currentTime();
        //时间转换成字符串
        //系统时间放入label
        ui->label->setText(sys_time.toString("hh::mm::ss"));
        ui->label->setAlignment(Qt::AlignCenter);
    }
}

void Widget::on_pushButton_clicked()
{
    if(ui->pushButton->text() == "启动")
    {
        id = startTimer(1000);//毫秒
        ui->pushButton->setText("关闭");
    }
    else
    {
        killTimer(id);
        ui->pushButton->setText("启动");
    }
}

void Widget::on_pushButton_2_clicked()
{
    if(ui->label->text() == ui->lineEdit->text())
    {
        for(int i=0;i<5;i++)
        {
             speecher->say(ui->lineEdit->text());
        }
    }
}
相关推荐
坐吃山猪2 分钟前
MFlow03-数据模型解析
开发语言·python·源码·agent·记忆
流年如夢6 分钟前
结构体:定义、使用与内存布局
c语言·开发语言·数据结构·c++·算法
thankseveryday7 分钟前
Three.js 把 Blender 绘制的曲线(Bezier / 曲线) 导入 Three.js 并作为运动路径 / 动画路径使用
开发语言·javascript·blender
Ulyanov8 分钟前
《玩转QT Designer Studio:从设计到实战》 QT Designer Studio动画与动效系统深度解析
开发语言·python·qt·系统仿真·雷达电子对抗仿真
兩尛14 分钟前
struct,union,Class,bitfield各自的作用和区别
java·开发语言
Gauss松鼠会26 分钟前
【openGauss】openGauss 磁盘引擎之 ustore
java·服务器·开发语言·前端·数据库·经验分享·gaussdb
键盘会跳舞27 分钟前
【Qt】分享一个笔者持续更新的项目: https://github.com/missionlove/NQUI
c++·qt·用户界面·qwidget
YSF2017_329 分钟前
C语言-13-制作动态库
c语言·开发语言
John.Lewis36 分钟前
Python小课(6)基础语法⑤
开发语言·python
csgo打的菜又爱玩42 分钟前
7.DispatcherResourceManagerComponentFactory解析.md
开发语言·python·flink