qt day 5

1>实现闹钟功能qt day 5_第1张图片

----------------------------------------------------------------------
.pro
----------------------------------------------------------------------
QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
----------------------------------------------------------------------
.h
----------------------------------------------------------------------
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void Localtime();
    void Clock_check();
    void Opbtn_Cli();
    void Clobtn_Cli();
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;

private:
    //显示实施时间定时器
    QTimer *loct;
    QTimer *clotime;

    //界面组件
    QLabel *loctimlab;
    QLineEdit *clocklab;
    QPushButton *opbtn;
    QPushButton *clobtn;
    QTextEdit *txt;

    //时间比较
    int lhour = 0;
    int lmin = 0;
    int lsec = 0;
    int chour = 0;
    int cmin = 0;
    int csec = 0;

    //鼠标位置
    QPoint clipos;
    QPoint movpos;

    //播报员
    QTextToSpeech *sp = new QTextToSpeech(this);

};
#endif // WIDGET_H
----------------------------------------------------------------------
main.cpp
----------------------------------------------------------------------
#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
----------------------------------------------------------------------
widget.cpp
----------------------------------------------------------------------
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->resize(700,500);
    this->setStyleSheet("background-color:skyblue");
    this->setWindowOpacity(0.8);
    //创建显示当前时间文本框
    loctimlab = new QLabel(this);
    loctimlab->move(50,50);
    loctimlab->resize(350,100);
    loctimlab->setAlignment(Qt::AlignCenter);
    QFont ff;
    ff.setPointSize(20);
    loctimlab->setFont(QFont(ff));

    //创建定时时间文本框
    clocklab = new QLineEdit(this);
    clocklab->move(loctimlab->x()+loctimlab->width()+50,loctimlab->y());
    clocklab->resize(200,45);
    ff.setPointSize(16);
    clocklab->setAlignment(Qt::AlignHCenter);
    clocklab->setFont(QFont(ff));
    clocklab->setText("00 : 00 : 00");


    //创建开启按钮
    opbtn = new QPushButton(this);
    opbtn->move(clocklab->x(),clocklab->y()+clocklab->height()+10);
    opbtn->resize(75,45);
    opbtn->setText("开启");
    connect(opbtn,&QPushButton::clicked,this,&Widget::Opbtn_Cli);

    //创建停止按钮
    clobtn = new QPushButton(this);
    clobtn->move(clocklab->x()+opbtn->width()+50,clocklab->y()+clocklab->height()+10);
    clobtn->resize(75,45);
    clobtn->setText("停止");
    clobtn->setEnabled(0);
    connect(clobtn,&QPushButton::clicked,this,&Widget::Clobtn_Cli);

    //创建提示文本框
    txt = new QTextEdit(this);
    txt->move(loctimlab->x(),loctimlab->y()+loctimlab->height()+50);
    txt->resize(600,250);
    txt->setText("knocking!!!");

    //实例化定时器
    loct = new QTimer(this);
    loct->start(1000);
    clotime = new QTimer(this);

    //连接定时器与槽函数
    connect(loct,&QTimer::timeout,this,&Widget::Localtime);

    connect(clotime,&QTimer::timeout,this,&Widget::Clock_check);
}

Widget::~Widget()
{
}

//获取实时时间
void Widget::Localtime()
{
    //获取实时时间
    QTime ltime = QTime::currentTime();

    //把时间显示到文本框上
    Widget::loctimlab->setText(ltime.toString("hh : mm : ss"));

    //获取时间
    lhour = ltime.hour();
    lmin = ltime.minute();
    lsec = ltime.second();

}

//时间比较
void Widget::Clock_check()
{
    if(lhour == chour && lmin == cmin && lsec == csec)
    {
        //阅读文本内容
        sp->say(txt->toPlainText());
    }
}

//开启闹钟定时
void Widget::Opbtn_Cli()
{
    //启动定时器
    clotime->start(1000);
    //获取定时时间
    QString tt;
    QByteArray t;
    QByteArray tmp;
    tt = clocklab->text();
    t = tt.toUtf8();
    int j = 0;
    //获取小时数
    for(int i = 0;t[j] != ':';i++,j++)
    {
        tmp[i] = t[j];
    }
    j++;
    chour = atoi(tmp);
    //获取分钟数
    for(int i = 0;t[j] != ':';i++,j++)
    {
        tmp[i] = t[j];
    }
    j++;
    cmin = atoi(tmp);
    //获取秒数
    for(int i = 0;t[j] != ':';i++,j++)
    {
        tmp[i] = t[j];
    }
    j++;
    csec = atoi(tmp);

    //设置其他为不可用
    opbtn->setEnabled(0);
    clocklab->setEnabled(0);
    txt->setEnabled(0);
    clobtn->setEnabled(1);

}

//停止按钮
void Widget::Clobtn_Cli()
{
    //设置按钮可用
    opbtn->setEnabled(1);
    clocklab->setEnabled(1);
    txt->setEnabled(1);
    clobtn->setEnabled(0);

    //停止定时器
    clotime->stop();
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
        clipos = event->pos();
    }
    else
    {
        this->close();
    }
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos()-clipos);
}

你可能感兴趣的:(qt,c++)