华清 Qt day3 9月19

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include       //键盘事件处理类的头文件
#include 
#include 
#include 
#include 
#include 
#include 


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 9月19_第1张图片

 QT - day3 - GitMind

面试题 - GitMind

你可能感兴趣的:(qt,java,开发语言)