华清 Qt day3 9月19

cpp 复制代码
QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
/*****************************************************************************/
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QFontDialog>
#include <QFont>
#include <QMessageBox>
#include <QDebug>
#include <QColorDialog>
#include <QColor>
#include <QFileDialog>
#include <QFile>
#include <QKeyEvent>      //键盘事件处理类的头文件
#include <QDebug>
#include <QMouseEvent>
#include <QTimerEvent>
#include <QTime>
#include <QTextToSpeech>
#include <QVoice>


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

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


private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    //重写定时器处理函数
    void timerEvent(QTimerEvent *e);

private:
    Ui::Widget *ui;

    int timer_id;       //定时器的id号

    QTextToSpeech *s;   //定义一个播报员

};
#endif // WIDGET_H

/**********************************************************************/
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    timer_id=0;
    s = new QTextToSpeech(this);  //给播报员申请空间
}

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

//启动定时器
void Widget::on_pushButton_clicked()
{
    timer_id = this->startTimer(1000);
    //功能: 启动一个定时器
    //参数: 每隔一定时间,自动调用定时器事件处理函数
}

//关闭定时器
void Widget::on_pushButton_2_clicked()
{
    this->killTimer(timer_id);
}

//定时器事件
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == timer_id)
    {
        QString userInputTime = ui->lineEdit->text();       //获取用户输入的时间
        QTime sys_t = QTime::currentTime();  //获取系统时间
        //将QTime类对象转换为字符串
        QString t = sys_t.toString("hh:mm:ss");    //转变成字符串
        ui->label->setText(t);  //打印到label栏

        if(t == userInputTime)
        {
            qDebug()<<"时间匹配成功";
            s->say(ui->textEdit->toPlainText());     //播报文本框里的内容
            this->killTimer(timer_id);       //关闭定时器
            timer_id=0;
        }
    }
}

/***********************************************************************/

QT - day3 - GitMind

面试题 - GitMind

相关推荐
hi星尘15 分钟前
深度解析:Java内部类与外部类的交互机制
java·开发语言·交互
看到我,请让我去学习16 分钟前
Qt编程-qml操作(js,c++,canvas)
开发语言·qt
橘子编程17 分钟前
Python-Word文档、PPT、PDF以及Pillow处理图像详解
开发语言·python
wuxinyan12323 分钟前
Java面试题033:一文深入了解MySQL(5)
java·数据库·mysql·面试
清心歌30 分钟前
Java SE线程的创建
java
高兴达1 小时前
Spring boot入门工程
java·spring boot·后端
Ronin3051 小时前
【C++】类型转换
开发语言·c++
萧曵 丶1 小时前
Spring @TransactionalEventListener
java·数据库·spring·事务·transactional·异步
笑衬人心。1 小时前
HTTPS详解:原理 + 加解密过程 + 面试问答
java·网络协议·http·面试·https
蓝澈11211 小时前
弗洛伊德(Floyd)算法-各个顶点之间的最短路径问题
java·数据结构·动态规划