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

相关推荐
用户805533698035 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner5 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz10 天前
QML Hello World 入门示例
qt
xcyxiner13 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner14 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner14 天前
DicomViewer (添加模型类)3
qt
xcyxiner15 天前
DicomViewer (目录调整) 2
qt
xcyxiner15 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能17 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G17 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt