Qt定时提示器

在这里插入图片描述

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>//定时器类头文件
#include <QString>
#include <QDebug>
#include <QTime>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QPixmap>
#include <QFontDialog>
#include <QTextToSpeech>
namespace Ui {
    
    
class Widget;
}

class Widget : public QWidget
{
    
    
    Q_OBJECT
public slots:
    void time_slot();
public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    QTimer *tm;
    void timerEvent(QTimerEvent *e)override;
private slots:
    void on_btn1_clicked();

    void on_btn2_clicked();
private:
    Ui::Widget *ui;
    int tid;
    QLabel *lb1,*lb2;
    QPushButton *bt1,*bt2;
    QTextEdit *ed,*ed1;
    QTextToSpeech *speecher;
    int flag=0;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    
    

    this->resize(700,400);
    this->setWindowFlags(Qt::FramelessWindowHint);
    lb1 = new QLabel(this);
    lb1 ->resize(700,400);
    lb1->setStyleSheet("background-image: url(:/1123.png);");
    lb2 = new QLabel(this);
    lb2 ->resize(180,40);
    lb2->setStyleSheet("background-color:#ffe06f;border-radius:5px;border:none;");
    lb2->move(80,60);
    ed = new QTextEdit(this);
    ed ->resize(240,40);
    ed->setFont(QFont("隶书",20,3));
    ed->setStyleSheet("background-color:#99ffc9;border-radius:5px;border:none;");
    ed->move(380,60);
    bt1 = new QPushButton("启动",this);
    bt1 ->resize(80,30);
    bt1->setStyleSheet("background-color:#cfff97;border-radius:5px;border:none;");
    bt1->move(400,120);
    bt2 = new QPushButton("关闭",this);
    bt2 ->resize(80,30);
    bt2->setStyleSheet("background-color:#cfff97;border-radius:5px;border:none;");
    bt2->move(530,120);
    ed1 = new QTextEdit(this);
    ed1 ->resize(440,40);
    ed1->setFont(QFont("隶书",20,3));
    ed1->setStyleSheet("background-color:#99ffc9;border-radius:5px;border:none;");
    ed1->move(100,260);
    //实例化定时器
    tm = new QTimer(this);
    //绑定到槽函数
    connect(tm,&QTimer::timeout,this,&Widget::time_slot);
    tm->start(10);
    connect(bt1,&QPushButton::clicked,this,&Widget::on_btn1_clicked);
    connect(bt2,&QPushButton::clicked,this,&Widget::on_btn2_clicked);
}

Widget::~Widget()
{
    
    
    delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
    
    
    if(e->timerId()==tid){
    
    
        QTime sys_time = QTime::currentTime();
        if(ed->toPlainText() == sys_time.toString("hh:mm:ss")){
    
    
            if(flag==1){
    
    
             speecher = new QTextToSpeech(this);
             speecher->say(ed1->toPlainText());
             flag=0;
            }
        }

    }
}

void Widget::on_btn1_clicked()
{
    
    
    if(bt1->text() == "启动"){
    
    
        flag = 1;
        tid = this->startTimer(100);
        ed1->setReadOnly(true);
        ed->setReadOnly(true);
        bt1->setText("取消");
    }else{
    
    
        flag = 0;
        ed1->setReadOnly(false);
        ed->setReadOnly(false);
        this->killTimer(tid);
        bt1->setText("启动");
    }
}

void Widget::on_btn2_clicked()
{
    
    
    this->close();
}

void Widget::time_slot()
{
    
    
    QTime sys_time = QTime::currentTime();
    QString now_time = sys_time.toString("hh:mm:ss");
    this->lb2->setText(now_time);
    this->lb2->setFont(QFont("隶书",20,3));
    this->lb2->setAlignment(Qt::AlignCenter);

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/tupkoo/article/details/131236404