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());
        }
    }
}
相关推荐
Quz1 天前
QML Hello World 入门示例
qt
xcyxiner4 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner5 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner5 天前
DicomViewer (添加模型类)3
qt
xcyxiner6 天前
DicomViewer (目录调整) 2
qt
xcyxiner6 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR0068 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术8 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园8 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob8 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio