9.2QTday4作业

1 闹钟

头文件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtDebug>
#include <QLabel>     //标签类
#include <QLineEdit>  //行编辑器类
#include <QPushButton> //按钮类
#include <QTextEdit>   //文本编辑器类
#include <QtTextToSpeech>  //文本转语音

#include <QTimer>     //定时器
#include <QTime>      //时间类

#include <QTimerEvent>  //定时器时间类头文件

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

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

public slots:
    void start_slot();    //自定义的槽函数,与启动按钮连接

    void stop_slot();     //自定义的槽函数,与停止按钮连接

private:
    Ui::Widget *ui;

    //定义一个"当前时间"的指针
    QLabel *current_time_lab;

    //定义一个"闹钟时间"的指针
    QLineEdit *clock_time;

    //定义"启动"和"停止"的指针
    QPushButton *start_btn;
    QPushButton *stop_btn;

    //定义一个"闹钟铃声"的指针
    QTextEdit *clock_voice;

    //定义一个播报员的指针
    QTextToSpeech *speecher;

    //定义一个定时器的指针
    QTimer *timer;

    //定义一个定时器id
    int time_id;

};
#endif // WIDGET_H

源文件

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置为纯净界面
    this->setWindowFlag(Qt::FramelessWindowHint);
    //设置闹钟界面固定大小
    this->setFixedSize(QSize(600,400));

    //给current_time_lab实例化空间
    current_time_lab = new QLabel(this);
    current_time_lab->setStyleSheet("background-color:skyblue");

    current_time_lab->resize(QSize(340,100));
    current_time_lab->move(30,40);

    //给colock_time实例化空间
    clock_time = new QLineEdit(this);
    clock_time->setAlignment(Qt::AlignCenter);   //文本对齐方式和字体
    clock_time->setFont(QFont("隶书", 10));
    clock_time->resize(QSize(160,50));
    clock_time->move(current_time_lab->x()+370,current_time_lab->y());

    //给start_btn 和 stop_btn实例化空间
    start_btn  = new QPushButton("启动",this);
    start_btn->resize(QSize(70,30));
    start_btn->move(clock_time->x(),clock_time->y()+70);

    stop_btn = new QPushButton("停止", this);
    stop_btn->resize(start_btn->size());
    stop_btn->move(start_btn->x()+90,start_btn->y());
    stop_btn->setEnabled(false);

    //给clock_voice实例化空间
    clock_voice = new QTextEdit("起床啦,起床啦,1234567asdfghjkl",this);
    clock_voice->setAlignment(Qt::AlignCenter);   //文本对齐方式和字体
    clock_voice->setFont(QFont("隶书", 20));
    clock_voice->resize(QSize(540,200));
    clock_voice->move(current_time_lab->x(),current_time_lab->y()+120);

    //给speecher实例化空间
    speecher = new QTextToSpeech(this);

    //实例化一个定时器timer  展示在当前时间界面
    timer = new QTimer(this);

    //启动定时器
    timer->start(1000);  //从此时起,每一秒定时器会发送timeout信号(连接槽函数处理)

    //将该定时的timeout信号连接到lambda表达式中中
    connect(timer, &QTimer::timeout, [&](){
            //调用QTime类的静态成员函数currentTime()获取系统时间
            QTime sys_time = QTime::currentTime();

            //将时间类对象调用成员函数toString()将sys_time转换成字符串
            QString time = sys_time.toString("hh:mm:ss");

            //将时间展示到current_time_lab界面上
            this->current_time_lab->setText(time);

            //将label上的内容居中展示,并设置字体
            this->current_time_lab->setAlignment(Qt::AlignCenter);
            this->current_time_lab->setFont(QFont("幼圆", 25, 15, false));

            //判断是否已到闹钟时间
            if(time == clock_time->text() && stop_btn->isEnabled())
            {

                //闹钟响起
                speecher->say(clock_voice->toPlainText());   //获取textedit编辑器文本需要使用toPlaiText();
            }
            //点击停止后,speecher调用stop停止闹钟声音
            else if(!stop_btn->isEnabled())
            {
                speecher->stop();
            }
    });

    //将启动按钮的信号与自定义的槽函数start_slot()连接
     connect(start_btn, &QPushButton::clicked, this, &Widget::start_slot);

    //将停止按钮的信号与槽函数stop_slot连接
    connect(stop_btn, &QPushButton::clicked, this, &Widget::stop_slot);
}

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


//与启动按钮连接到槽函数的定义
void Widget::start_slot()
{

    //将闹钟时间编辑器、启动按钮、闹钟声音对应的文本编辑器设置为不可用
    clock_time->setEnabled(false);
    start_btn->setEnabled(false);
    clock_voice->setEnabled(false);

    stop_btn->setEnabled(true);
}

//与停止按钮连接到槽函数的定义
void Widget::stop_slot()
{
   //将闹钟时间编辑器、启动按钮、闹钟声音对应的文本编辑器设置为可用
    clock_time->setEnabled(true);
    start_btn->setEnabled(true);
    clock_voice->setEnabled(true);

    stop_btn->setEnabled(false);
}

2 思维导图

相关推荐
AAA废品回收站陈师傅1 小时前
68常用控件_QGroupBox的使用
qt
明月醉窗台1 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
沉到海底去吧Go2 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
奥修的灵魂6 小时前
QT进阶之路:带命名空间的自定义控件在Qt设计器与qss中的使用技巧
qt·命名空间
笨笨马甲10 小时前
附加模块--Qt OpenGL模块功能及架构
开发语言·qt
uyeonashi14 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
galaxy_strive1 天前
绘制饼图详细过程
开发语言·c++·qt
委婉待续1 天前
Qt的学习(一)
开发语言·qt·学习
笨笨马甲1 天前
Qt Quick Layout功能及架构
开发语言·qt
feiyangqingyun1 天前
Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动
c++·qt·udp·gb28181