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

运行效果

相关推荐
WWJA王文举5 小时前
I²C通信完整流程详解:START、地址、ACK、数据、Repeated START和STOP一次讲透
c语言·开发语言
zhanghaha13145 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
qq_448011167 小时前
C语言中的动态内存分配
c语言·开发语言·php
汉字萌萌哒7 小时前
2024CSP-J入门级C++真题详解
开发语言·c++
arbboter7 小时前
【网络工具】NetProxy网络代理用户手册
开发语言·网络·c#·网络代理·网络异常·代理工具
汉字萌萌哒8 小时前
2019CCF-CSP入门级C++试题解析
java·开发语言·c++
叠层归一研究院9 小时前
AGI 系统(十一):二阶网络 — 二阶递归 × 多主体 (元符号网络)
开发语言·人工智能·算法·php·agi
小雨笙笙9 小时前
C语言:变量三属性——存储类型
c语言·开发语言
果果燕10 小时前
C++ 学习笔记(二):类与对象—— 构造函数与析构函数
开发语言·c++
江畔柳前堤10 小时前
Function Calling 与 Tool Calling:从认知到工程的全景深度解析
开发语言·网络·人工智能·深度学习·算法·机器学习·php