3.26 day5 QT

头文件:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent> //时间事件
#include <QTime> //时间类
#include <QTextToSpeech> //文本转语音

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

    void timerEvent(QTimerEvent *e);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    int id,id2;
    QString s;
    QTextToSpeech *speech;
};

#endif // WIDGET_H

源文件:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    speech(new QTextToSpeech (this))
{
    ui->setupUi(this);
    QTime sys_time = QTime::currentTime();//获取当前系统时间
    s = sys_time.toString("hh:mm:ss");
    ui->systimelabel->setText(s);
    ui->systimelabel->setAlignment(Qt::AlignCenter);//居中显示
    id = startTimer(1000);
}

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

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == id)
    {
        QTime sys_time = QTime::currentTime();
        s = sys_time.toString("hh:mm:ss");//获取当前系统时间
        ui->systimelabel->setText(s);
        ui->systimelabel->setAlignment(Qt::AlignCenter);
    }
    else if(e->timerId() == id2)
    {
        if(ui->systimelabel->text() == ui->lineEdit->text())
        {
            for (int i = 0; i < 5; i++)
            {
                ui->label->setText("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
                speech->say("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
            }
        }
    }
}

void Widget::on_pushButton_clicked()
{
    if(ui->pushButton->text() == "启动")
    {
        id2 = startTimer(1000);
        ui->pushButton->setText("关闭");

    }else {
        ui->pushButton->setText("启动");
        killTimer(id2);
    }
}

相关推荐

  1. <span style='color:red;'>QT</span> <span style='color:red;'>day</span><span style='color:red;'>5</span>

    QT day5

    2024-03-27 05:44:02      34 阅读
  2. <span style='color:red;'>QT</span> <span style='color:red;'>day</span><span style='color:red;'>5</span>

    QT day5

    2024-03-27 05:44:02      16 阅读
  3. <span style='color:red;'>day</span><span style='color:red;'>5</span>-<span style='color:red;'>QT</span>

    day5-QT

    2024-03-27 05:44:02      16 阅读
  4. <span style='color:red;'>day</span><span style='color:red;'>5</span> <span style='color:red;'>qt</span>

    day5 qt

    2024-03-27 05:44:02      15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-27 05:44:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-27 05:44:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-27 05:44:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-27 05:44:02       20 阅读

热门阅读

  1. 量子计算与大模型融合的潜力与挑战探索

    2024-03-27 05:44:02       16 阅读
  2. 简明 Python 教程(第5章 函数)

    2024-03-27 05:44:02       18 阅读
  3. Python 和 Go:一文了解

    2024-03-27 05:44:02       18 阅读
  4. 151 shell编程

    2024-03-27 05:44:02       16 阅读
  5. React中的受控组件与非受控组件

    2024-03-27 05:44:02       17 阅读
  6. go | switch/case、array、slices、range、functions

    2024-03-27 05:44:02       16 阅读
  7. DMA知识

    DMA知识

    2024-03-27 05:44:02      16 阅读
  8. IOS面试题编程机制 71-75

    2024-03-27 05:44:02       16 阅读
  9. 【CSP试题回顾】202104-2-邻域均值(优化)

    2024-03-27 05:44:02       17 阅读
  10. 大话设计模式之工厂模式

    2024-03-27 05:44:02       19 阅读