qt作业day4

复制代码
//clock_exercise.cpp

#include "clock_timer.h"
#include "ui_clock_timer.h"



//时间事件处理函数
void Clock_Timer::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == time_id)
    {
        sys_tm = QDateTime :: currentDateTime();
//        int year = sys_tm.date().year();
//        int month = sys_tm.date().month();
//        int day = sys_tm.date().day();

        //时分秒
        int hour = sys_tm.time().hour();
        int min = sys_tm.time().minute();
        int sec = sys_tm.time().second();
//        qDebug()  << "系统时间:" << hour << ":" << min << ":" << sec;
        QString settime = timeEdit->text();
        this->timeLab->setText(sys_tm.toString("yyyy-mm-dd hh:mm:ss"));
        splitTime();
        QString readtxt =this->txt_edit->toPlainText();
        vector<int> :: iterator iter = split.begin();

        if(*(iter++) == hour && *(iter++) == min && *iter == sec)
        {
            speech->say(this->txt_edit->toPlainText());
        }

    }
}

Clock_Timer::Clock_Timer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Clock_Timer)
{
    ui->setupUi(this);
    this->resize(800,700);
    this->setWindowFlags(Qt :: FramelessWindowHint);
//    this->setWindowTitle("小淼子快起床了");

    //显示系统时间标签
    timeLab = new QLabel(this);
    timeLab->move(30,20);
    timeLab->resize(300,60);
    timeLab->setText("系统时间");
    timeLab->setFont(QFont("楷体",16));
    timeLab->setStyleSheet("background-color:skyblue");


    //启动按钮
    startBtn = new QPushButton(this);
    startBtn->setText("启动");
    startBtn->move(600,50);
    startBtn->resize(QSize(60,40));
    connect(startBtn,&QPushButton :: clicked,this,&Clock_Timer :: startRecTime);

    //停止按钮
    stopBtn = new QPushButton(this);
    stopBtn->resize(startBtn->size());
    stopBtn->move(startBtn->x() + startBtn->width() + 20,startBtn->y());
    stopBtn->setText("停止");
    stopBtn->setEnabled(false);
    connect(stopBtn,&QPushButton :: clicked,this,&Clock_Timer :: stopRecTime);


    //用户输入时间单行编辑器
    timeEdit = new QLineEdit(this);
    timeEdit->move(400,timeLab->y());
    timeEdit->setFont(QFont("楷体",16));
    timeEdit->resize(200,80);
    timeEdit->setPlaceholderText("时:分:秒");

    //用户输入语音播报文本编辑器
    txt_edit = new QTextEdit(this);
    txt_edit->move(0,200);
    txt_edit->resize(QSize(this->width(),this->height() - 200));
    txt_edit->setFont(QFont("楷体",20));

    //语音播报
    speech = new QTextToSpeech(this);

}

void Clock_Timer::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt :: LeftButton)
    {
        this->move(event->globalPos() - startPos);
    }

}

void Clock_Timer::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt :: LeftButton)
    {
        this->startPos = event->globalPos() - this->pos();
    }
    else if(event->buttons() == Qt :: RightButton)
    {
        int ret = QMessageBox :: question(this,"确认","是否要关闭界面",QMessageBox :: Yes | QMessageBox :: No,QMessageBox :: No);
        if(ret == QMessageBox :: Yes)
        {
            this->close();
        }
    }

}

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


//按下启动按钮后的事件处理函数
void Clock_Timer::startRecTime()
{
//    qDebug() << this->timeEdit->text();
    if(this->timeEdit->text() == "")
    {
        QMessageBox :: warning(this,"警告","无输入",QMessageBox :: Ok,QMessageBox :: Ok);
        return;
    }

    //点击启动后,解锁停止按钮,同时锁定启动按钮
    this->stopBtn->setEnabled(true);
    this->startBtn->setEnabled(false);
    this->timeEdit->setEnabled(false);
    this->txt_edit->setEnabled(false);

    //开启定时器
    time_id = this->startTimer(1000);
}


//按下停止按钮后的事件处理函数
void Clock_Timer::stopRecTime()
{
    int ret = QMessageBox :: question(this,"提示","是否确认要停止定时器",QMessageBox :: Yes | QMessageBox :: No,QMessageBox :: Yes);
    if(ret == QMessageBox :: Yes)
    {
        this->startBtn->setEnabled(true);
        this->stopBtn->setEnabled(false);
        this->timeEdit->setEnabled(true);
        this->txt_edit->setEnabled(true);

        //关闭定时器
        this->killTimer(time_id);
    }

}

//分割时分秒
void Clock_Timer :: splitTime()
{
    this->split.clear();
    QString temp = timeEdit->text();
    QStringList s = temp.split(":");

    QStringList :: iterator iter = s.begin();
    for(;iter != s.end();iter++)
    {
        qDebug() << (*iter);
        split.push_back(iter->toInt());
    }
}


//头文件

#ifndef CLOCK_TIMER_H
#define CLOCK_TIMER_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QTime>
#include <QTextToSpeech>
#include <QDateTime>
#include <QMessageBox>
#include <vector>
#include <QDebug>
#include <QMouseEvent>


using namespace std;
namespace Ui {
class Clock_Timer;
}

class Clock_Timer : public QWidget
{
    Q_OBJECT


void timerEvent(QTimerEvent *event);    //时间事件处理函数


public:
    explicit Clock_Timer(QWidget *parent = nullptr);
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    ~Clock_Timer();
    void splitTime();
public slots:
    void startRecTime();
    void stopRecTime();

private:
    Ui::Clock_Timer *ui;

    QPushButton *startBtn;      //启动按钮
    QPushButton *stopBtn;       //停止按钮
    QLabel *timeLab;            //系统获取时间
    QLineEdit *timeEdit;       //记录用户输入的时间
    QTextEdit *txt_edit;        //用户输入的播报内容
    QMessageBox *stopConfirm;   //对话框,询问用户是否确认停止
    QTextToSpeech *speech;
    int time_id;                //记录定时器ID
    QDateTime sys_tm;           //记录系统时间
    vector<int> split;
    QPoint startPos;               //记录鼠标起始点

};

#endif // CLOCK_TIMER_H

运行效果

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