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

运行效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值