7.27 作业 QT

要求:

 结果图:

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 <QWidget>
#include <QTimerEvent>  //定时器事件处理函数
#include <QTime>    //时间类
#include <QPushButton>  //按钮类头文件
#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_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:

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

思维导图:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值