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 思维导图

相关推荐
Eoneanyna1 小时前
QT设置git仓库
开发语言·git·qt
Langneer5 小时前
Qt 状态机编程,双层状态机,实现暂停恢复
开发语言·qt
三玖诶5 小时前
如何在 Qt 的 QListWidget 中为某一行添加点击事件
开发语言·qt
InJre5 小时前
QT widgets 窗口缩放,自适应窗口大小进行布局
开发语言·qt·ui
冷凝女子6 小时前
【QT】基于HTTP协议的网络应用程序
开发语言·qt·http
QT界面美化6 小时前
QT硬件通讯基础
qt·qt6·qt quick
kgduu8 小时前
Qt之QFuture理解
qt
mengzhi啊9 小时前
qt七个按钮进行互斥
qt
ljp_nan10 小时前
QT --- 初识QT
开发语言·qt
十启树12 小时前
用Qt 对接‌百度AI平台
人工智能·qt·百度