【Qt】实现一个小闹钟

widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>//按钮类
#include <QLineEdit>//行编辑器
#include <QLabel>//标签类
#include <QTextEdit>//输入框
#include <QTimerEvent>//定时器事件类
#include <QTimer>            //定时器类
#include <QTime>            //时间类
#include <QTimerEvent>       //定时器事件类
#include <QTextToSpeech>    //语音播报
#include <QMouseEvent>      //鼠标事件
#include <QMessageBox>      //提示框
#include <QObject>
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 *event)override;
    void mouseMoveEvent(QMouseEvent *event) override;//鼠标移动事件
    void mousePressEvent(QMouseEvent *event) override;//鼠标点击事件


public slots:
    void click_startBtn();
    void click_cancelBtn();


private:
    Ui::Widget *ui;

    QPushButton *startBtn;  //启动按钮
    QPushButton *cancelBtn; //取消按钮
    QLabel  *lab1;          //显示系统时间
    QLineEdit *lin1;        //时间输入框
    QTextEdit *tex1;        //输入框
    QTextToSpeech  *tts;    //语音播报
    QTimer t1;              //定义一个定时器变量
    QPoint temp;            //中间向量

    int tid = 0;            //输入时间时候使用

};
#endif // WIDGET_H

widget.cpp

复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->resize(500,400);//设置窗口大小
    this->setWindowFlag(Qt::FramelessWindowHint);//设置去除窗口标题


    //显示系统时间组件的一系列操作
    lab1 = new QLabel(this);
    lab1->resize(200,100);//设置大小
    lab1->setStyleSheet("background-color:pink;border-radius:10;font-size: 15pt;");
    lab1->move(20,20);//移动
    t1.start(1000);//启动一个定时器
    lab1->setAlignment(Qt::AlignCenter);//设置居中
    //显示系统时间的逻辑
    connect(&t1,&QTimer::timeout,[&](){
        lab1->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));

    });


    //对可编辑时间lin1组件的操作
    lin1 = new QLineEdit(this);
    lin1->resize(250,30);//设置大小
    lin1->move(lab1->x()+lab1->width()+10,lab1->y());//移动
    lin1->setPlaceholderText("请输入时间");//设置占位符

    //对启动按钮组件的操作
    startBtn = new QPushButton(this);
    startBtn->setText("启动");//设置默认文本
    startBtn->resize(100,50);//设置大小
    startBtn->move(lin1->x(),lin1->y()+lin1->height()+20);
    startBtn->setStyleSheet("background-color:rgb(128, 128, 128);border-radius:10;");
    connect(startBtn,&QPushButton::clicked,this,&Widget::click_startBtn);

    //对取消按钮组件进行操作
    cancelBtn = new QPushButton(this);//开辟空间
    cancelBtn->setText("取消");
    cancelBtn->resize(100,50);
    cancelBtn->move(startBtn->x()+startBtn->width()+50,startBtn->y());
    cancelBtn->setStyleSheet("background-color:rgb(128, 128, 128);border-radius:10;");
    connect(cancelBtn,&QPushButton::clicked,this,&Widget::click_cancelBtn);

    //对语音框组件进行操作
    tex1 = new QTextEdit(this);
    tex1->resize(500,400);
    tex1->move(0,150);
    tex1->setPlaceholderText("请输入你要朗读的内容");

    tts = new QTextToSpeech();
}
//启动逻辑槽函数
void Widget::click_startBtn()
{
    //启动定时器
    tid = this->startTimer(1000);
    lin1->setReadOnly(true);//禁用
    tex1->setReadOnly(true);//禁用
    qDebug()<<"启动成功";
}
//取消逻辑槽函数
void Widget::click_cancelBtn()
{
    int res = QMessageBox::information(this,
                                        "取消",
                                        "确定要取消嘛?",
                                         QMessageBox::Yes|QMessageBox::No,
                                        QMessageBox::No);

    if(res == QMessageBox::Yes)
    {
        this->killTimer(tid);//关闭定时器
        lin1->setReadOnly(false);//解除禁用
        tex1->setReadOnly(false);//解除禁用
        qDebug()<<"取消成功";
    }

}

//定时器处理函数
void Widget::timerEvent(QTimerEvent *event)
{
    QString text = lab1->text();
    if(event->timerId() == tid)
    {
        //如果两者相等就语音语音播报
        if(lin1->text() == lab1->text())
        {
            tts->say(tex1->toPlainText());
            qDebug()<<"语音播报成功";
        }
    }

}
//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos()-temp);
}
//鼠标点击事件
void Widget::mousePressEvent(QMouseEvent *event)
{
    temp = event->globalPos()-this->pos();
    //点击鼠标左键关闭
    if(event->button() == Qt::RightButton)
    {
        this->close();
    }
}

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

效果展示:

相关推荐
Scott9999HH2 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社3 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海3 小时前
python 列表、元组、集合和字典
开发语言·python
萧瑟余晖4 小时前
JDK 26 新特性详解
java·开发语言
马优晨5 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
人邮异步社区7 小时前
怎么把C语言学到精通?
c语言·开发语言
心平气和量大福大7 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai7 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
এ慕ོ冬℘゜8 小时前
前端基础:什么是时间戳?JS获取时间戳三种方法与实战用途
开发语言·前端·javascript
执明wa8 小时前
LayoutInflater详解: XML是如何变成View的?
android·xml·开发语言·android studio