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());
        }
    }
}
相关推荐
LDR0061 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术1 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园1 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob1 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享1 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.1 天前
C语言--day30
c语言·开发语言
何以解忧,唯有..1 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
謓泽1 天前
C语言不是语法,是通往机器的地图。
c语言·开发语言
云水一下1 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
飞天狗1111 天前
零基础JavaWeb入门——第五课第二小节:九大内置对象 · 第2个:response(响应对象)
java·开发语言