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 :: 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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


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 split;
    QPoint startPos;               //记录鼠标起始点

};

#endif // CLOCK_TIMER_H

运行效果

qt作业day4_第1张图片

qt作业day4_第2张图片

你可能感兴趣的:(qt,开发语言)