7.27 作业 QT

要求:

7.27 作业 QT_第1张图片

 结果图:

7.27 作业 QT_第2张图片

clock.pro:

QT       += core gui
QT += 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

 widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#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();

    void timerEvent(QTimerEvent *e);    //要重写的关于定时器事件处理函数的声明

private slots:
    void on_start_Btn_clicked();

    void on_stop_Btn_clicked();

private:
    Ui::Widget *ui;

    static int count;   //定义静态变量

    QTextToSpeech *speecher;  //定义一个播报者

    //定义一个基于事件处理的定时器id
    int tid1;
    int tid2;
};
#endif // WIDGET_H

widget.cpp:

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    tid1 = startTimer(1000);    //启动一个定时器,隔1s会自动执行timerevent函数

    //设置占位符
    ui->clock_lineEdit->setPlaceholderText("输入多少秒后开启闹铃");

    speecher = new QTextToSpeech(this);
}

int Widget::count = 0;

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

void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == tid1)
    {
             //获取系统事件
          QTime sys_time = QTime::currentTime();  //QTime类对象

            //将时间转化为字符串
          QString t = sys_time.toString("hh:mm:ss");

         //将字符串展示到ui界面
          ui->time_lab->setText(t);
          ui->time_lab->setAlignment(Qt::AlignHCenter);

    }else if(event->timerId() == tid2)
    {
        count++;

        //将clock_lineEdit 的文本转化为整数
        int m = ui->clock_lineEdit->text().toInt();

        if(count == m)
        {
            //进行语音播报
            speecher->say(ui->read_textEdit->toPlainText());

            //停止计时器
            killTimer(tid2);
        }
    }

}

void Widget::on_start_Btn_clicked()
{

    //将start_Btn设置成不可用状态
    ui->start_Btn->setEnabled(false);

    //将clock_lineEdit设置成不可用状态
    ui->clock_lineEdit->setEnabled(false);

    //将stop_Btn设置成可用状态
    ui->stop_Btn->setEnabled(true);

    //启动第二个计时器
    tid2 = startTimer(1000);    //启动一个定时器,隔1s会自动执行timerevent函数

    //将count置零
    count = 0;

}

void Widget::on_stop_Btn_clicked()
{
    //停止语音播报
    speecher->stop();

    //将stop_Btn设置成不可用状态
    ui->stop_Btn->setEnabled(false);

    //将start_Btn设置成可用状态
    ui->start_Btn->setEnabled(true);
    ui->clock_lineEdit->setEnabled(true);

    //停止计时器
    killTimer(tid2);

    //将count置零
    count = 0;
}

widget.ui:



 Widget
 
  
   
    0
    0
    800
    613
   
  
  
   Widget
  
  
   
    
     50
     60
     321
     161
    
   
   
    
     22
     75
     false
     true
     true
    
   
   
    background-color: rgb(85, 255, 255);
   
   
    
   
  
  
   
    
     430
     60
     291
     71
    
   
   
    background-color: rgb(255, 255, 0);
   
  
  
   
    
     430
     160
     121
     61
    
   
   
    开始
   
  
  
   
    
     590
     160
     121
     61
    
   
   
    停止
   
  
  
   
    
     50
     250
     671
     341
    
   
   
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:18pt; color:#55ffff;">故人西辞黄鹤楼,唱跳Rap打篮球。春风又绿江南岸,练习长达两年半。清明时节雨坤坤,路上行人梳中分,借问背带何处有,牧童遥指我爱坤。长江后浪推前浪,爱坤啥样我啥样。鸡冠头背带裤,我是爱坤你记住。向阳花木易为春,听说你爱蔡徐坤。小黑子树枝666,蒸虾头,好丸吗?树枝赶人,蒸五鱼,食不食油饼,我家鸽鸽在胎上拿姜拿到手软,他那么努力,这样叫我怎么荔枝?已经橘爆你了,再这样我就抱紧了,劝你苏珊,不然我们爱坤就紫砂了。</span></p></body></html>
   
  
 
 
 

思维导图:

7.27 作业 QT_第3张图片

 

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