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());
        }
    }
}
相关推荐
LjQ20403 分钟前
网络爬虫一课一得
开发语言·数据库·python·网络爬虫
你是狒狒吗10 分钟前
TM中,return new TransactionManagerImpl(raf, fc);为什么返回是new了一个新的实例
java·开发语言·数据库
勤奋的知更鸟21 分钟前
Java编程之组合模式
java·开发语言·设计模式·组合模式
虾球xz28 分钟前
CppCon 2015 学习:3D Face Tracking and Reconstruction using Modern C++
开发语言·c++·学习·3d
林鸿群30 分钟前
C#子线程更新主线程UI及委托回调使用示例
开发语言·c#
奥修的灵魂42 分钟前
QT进阶之路:带命名空间的自定义控件在Qt设计器与qss中的使用技巧
qt·命名空间
SteveDraw3 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
十五年专注C++开发3 小时前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
flyair_China4 小时前
【云架构】
开发语言·php