Qt day4作业

1.

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->resize(600,500);
    this->setWindowTitle("闹钟");
    this->setWindowFlag(Qt::FramelessWindowHint);
    opbtn = new QPushButton("启动",this);
    stbtn = new QPushButton("停止",this);
    clbtn = new QPushButton("❌",this);
    //启动按钮
    opbtn->resize(90,30);
    opbtn->move(350,100);
    //停止按钮
    stbtn->resize(90,30);
    stbtn->move(460,100);
    stbtn->setEnabled(false);
    //关闭按钮
    clbtn->resize(30,30);
    clbtn->move(570,0);
    //行编辑器
    ledit = new QLineEdit(this);
    ledit->resize(200,50);
    ledit->move(350,30);
    QDateTime sys_time = QDateTime::currentDateTime();
    this->ledit->setText(sys_time.toString("yyyy-MM-dd hh:mm:ss"));
    ledit->setAlignment(Qt::AlignCenter);
    ledit->setFont(QFont("微软雅黑",10));
    //文本编辑器
    tedit = new QTextEdit(this);
    tedit->setPlaceholderText("请输入内容");
    tedit->resize(580,350);
    tedit->move(10,140);
    tedit->setFont(QFont("微软雅黑",14));
    //标签
    label = new QLabel(this);
    label->resize(300,100);
    label->move(30,30);
    //标签内时间的定时器
    timer_id = this->startTimer(1000);
    //播音员
    speecher = new QTextToSpeech(this);
    //连接槽函数
    connect(this->clbtn,&QPushButton::clicked,this,&Widget::on_clbtn_clicked);
    connect(this->opbtn,&QPushButton::clicked,this,&Widget::on_opbtn_clicked);
    connect(this->stbtn,&QPushButton::clicked,this,&Widget::on_stbtn_clicked);
    connect(this->opbtn,&QPushButton::clicked,this,&Widget::fun);
}

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

void Widget::mousePressEvent(QMouseEvent *event)
{
    drap = event->globalPos() - this->pos();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos()-drap);
}

void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == timer_id)
    {
        QDateTime sys_time = QDateTime::currentDateTime();
        this->label->setText(sys_time.toString("yyyy-MM-dd hh:mm:ss"));
        label->setAlignment(Qt::AlignCenter);
        label->setFont(QFont("微软雅黑",16));
    }
    if(event->timerId() == timer_id2)
    {
        QString ld = ledit->text();
        QString l = label->text();
        QString td = tedit->toPlainText();
        if(ld == l)
        {
            QMessageBox box(QMessageBox::Information,
                            "响铃",
                            "闹钟响铃了",
                            QMessageBox::Ok,
                            this);
            box.exec();
            speecher->say(td);
        }
    }
}

void Widget::on_clbtn_clicked()
{
    QMessageBox box(QMessageBox::Warning,
                    "警告",
                    "是否退出闹钟?",
                    QMessageBox::Yes|QMessageBox::No,
                    this);
    box.setDefaultButton(QMessageBox::No);
    box.setButtonText(QMessageBox::No,"否");
    box.setButtonText(QMessageBox::Yes,"是");
    int ret = box.exec();
    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
}

void Widget::on_opbtn_clicked()
{
  stbtn->setEnabled(true);
  opbtn->setEnabled(false);
  ledit->setEnabled(false);
  tedit->setEnabled(false);
}

void Widget::on_stbtn_clicked()
{
    stbtn->setEnabled(false);
    opbtn->setEnabled(true);
    ledit->setEnabled(true);
    tedit->setEnabled(true);
    disconnect(this->opbtn,&QPushButton::clicked,this,&Widget::fun);
}

void Widget::fun()
{
    timer_id2 = this->startTimer(1000);
}
#ifndef WIDGET_H
#define WIDGET_H

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

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void timerEvent(QTimerEvent *event) override;

public slots:
    void on_clbtn_clicked();
    void on_opbtn_clicked();
    void on_stbtn_clicked();
    void fun();

private:
    Ui::Widget *ui;
    QPoint drap;
    QPushButton *opbtn;
    QPushButton *stbtn;
    QPushButton *clbtn;
    QLineEdit *ledit;
    QTextEdit *tedit;
    QLabel *label;
    int timer_id,timer_id2;
    QTextToSpeech *speecher;

};
#endif // WIDGET_H

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