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());
        }
    }
}
相关推荐
FL162386312921 小时前
[C#][winform]基于yolov8的水表读数检测与识别系统C#源码+onnx模型+评估指标曲线+精美GUI界面
开发语言·yolo·c#
cnxy1881 天前
围棋对弈Python程序开发完整指南:步骤1 - 棋盘基础框架搭建
开发语言·python
程序员-周李斌1 天前
Java 死锁
java·开发语言·后端
JasmineWr1 天前
CompletableFuture相关问题
java·开发语言
零雲1 天前
java面试:知道java的反射机制吗
java·开发语言·面试
Jeremy爱编码1 天前
实现 Trie (前缀树)
开发语言·c#
laocooon5238578861 天前
插入法排序 python
开发语言·python·算法
你的冰西瓜1 天前
C++中的list容器详解
开发语言·c++·stl·list
就不掉头发1 天前
I/O复用
运维·服务器·c语言·开发语言
梦里小白龙1 天前
JAVA 策略模式+工厂模式
java·开发语言·策略模式