【QT】Day3

1. 完成闹钟的实现:

widgt.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
#include <QTimerEvent>  //定时器事件处理函数
#include <QTime>    //时间类
#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_eventStartBtn_clicked();
   void on_stopBtn_clicked();

private:
    Ui::Widget *ui;

    QString t1;
    int tid1 = startTimer(500);
    int tid2;//定义闹钟事件处理的定时器id
    QTextToSpeech *speecher;       //定义一个播报者
};
#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);

    //实例化一个播报员
    speecher = new QTextToSpeech(this);
//    QColor c = QColorDialog::getColor(QColor(0,255,255),   //初始颜色
//                                      this,                //父组件
//                                      "选择颜色");          //窗口标题
//   ui->textEdit->setTextColor(c);   //设置字体颜色,前景色
    ui->edit->setTextColor(QColor(66,90,240));
    ui->edit->setFont(QFont("隶书",15,10,false));
}

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


void Widget::on_eventStartBtn_clicked()      //"启动"按钮
{
     //启动一个定时器
     tid2 = startTimer(1000); //每隔1000ms会自动执行timerEvent函数
}


void Widget::on_stopBtn_clicked()   //"停止" 按钮
{
    ui->clockEdit->clear(); //清空闹钟时间
    ui->edit->clear();      //清空提示内容
    this->killTimer(tid2);    //关闭闹钟定时器
}


//定时器事件处理函数的定义
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == tid1) //系统时间定时器,1s刷新
    {
        //设置系统时间
        QTime sys_time = QTime::currentTime();  //QTime类对象
        t1 = sys_time.toString("hh:mm:ss");    //将时间转换成字符串

        //将字符串展示到ui界面
        ui->sysTimeLab->setText(t1);
        ui->sysTimeLab->setFont(QFont("隶书",15,10,false));
        ui->sysTimeLab->setAlignment(Qt::AlignCenter);  //居中显示
    }

    if(e->timerId() == tid2) //闹钟定时器
    {
        //从ui界面上的读取下来
        QString t2 = ui->clockEdit->text();
        if(t2 == t1 )
        {
            ui->edit->setText("三更灯火五更鸡,"
                              "正是男儿读书时,"
                              "黑发不知勤学早,"
                              "白首方悔读书少");
            speecher->say(t2);
            speecher->say(ui->edit->toPlainText());
        }
    }
}
  1. 思维导图
相关推荐
米码收割机17 分钟前
【Python】Python Django+Vue3校园自习室预约管理系统(源码+文档+PPT)【独一无二】
开发语言·python·django
AstartesEternal25 分钟前
python第二次作业(列表,字典)
开发语言·python
星恒随风33 分钟前
C++ STL 详解:set 与 multiset 的使用、区间查询和算法应用
开发语言·c++·笔记·学习·算法
数聚天成DeepSData41 分钟前
外贸海关进出口数据去哪免费下载?从统计到明细的查找指南
linux·服务器·开发语言·前端·网络·人工智能·自然语言处理
再卷也是菜43 分钟前
C++11支持并发库
开发语言·c++
Lhan.zzZ1 小时前
QML 开发中的“陷阱”:动态模型更新时,ComboBox 索引为何总是失效?
开发语言·qt
小园子的小菜1 小时前
Java 并发四大工具类深度解析:CountDownLatch、CyclicBarrier、Semaphore、Exchanger 原理、源码与面试考点
java·开发语言·面试
河西石头1 小时前
再谈C#的抽象类和接口:从“适配器”到“毛坯房”的设计哲学
开发语言·c#·接口·依赖注入·抽象类·依赖倒挂
颜x小1 小时前
[C#]——接口与继承
开发语言·c++·c#
冻柠檬飞冰走茶1 小时前
PTA基础编程题目集 7-17 爬动的蠕虫(C语言实现)
c语言·开发语言·数据结构·算法