QT DAY4

作业:要求做一个闹钟

clock.pro
QT       += core gui texttospeech

main.cpp
#include "widget.h"



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->speecher=new QTextToSpeech(this);
    this->timer=new QTimer(this);
    timer->start(1000);

    connect(timer,&QTimer::timeout,[&]()
    {
        //将QTime类对象->字符串
        QString currentTime =QTime::currentTime().toString("hh:mm:ss");
        lab1->setText(currentTime); //设置显示系统时间
        if(alarmSet&¤tTime==lint1->text())
        {
            speecher->say(lint2->text());
        }
    });

    this->resize(800,600);  //重设窗体size
    this->setWindowFlag(Qt::FramelessWindowHint);  //设置框体隐藏
    this->lab2=new QLabel(this);//用于放背景图
    lab2->resize(800,600);
    lab2->setPixmap(QPixmap(":/1.jpg"));
    lab2->setScaledContents(true);


    this->lab1=new QLabel(this);//显示系统时间
    lab1->resize(360,200);
    lab1->move(50,50);
    lab1->setAlignment(Qt::AlignCenter);
    QFont f1("宋体",30);
    lab1->setFont(f1);
    lab1->setStyleSheet("QLabel{color:rgba(234,165,67,255);border-radius:10;}");
    lab1->setText(QTime::currentTime().toString("hh:mm:ss"));
    lab1->raise();



    this->lint1=new QLineEdit(this);//一号编辑框用于设置闹钟时间
    lint1->resize(300,90);
    lint1->move(440,80);
    f1.setItalic(true);
    lint1->setFont(f1);
    lint1->setPlaceholderText("请输入闹钟时间");

    this->lint2=new QLineEdit(this);//二号编辑框用于设置语音播报文本
    lint2->resize(690,311);
    lint2->move(50,260);
    lint2->setAlignment(Qt::AlignLeft);
    lint2->setAlignment(Qt::AlignTop);
    QFont f2("楷体",15);
    lint2->setFont(f2);
    lint2->setText("三更灯火五更鸡,正式男儿读书时,黑发不知勤学早,白首方悔读书迟");


    this->btn1=new QPushButton("启动",this);//启动按钮
    btn1->resize(130,45);
    btn1->move(450,200);
    btn1->setStyleSheet("background-color:pink;border-radius:10;");
    btn1->setEnabled(true);   //设置按钮可用

    this->btn2=new QPushButton("取消",this);//取消按钮
    btn2->resize(130,45);
    btn2->move(600,200);
    btn2->setStyleSheet("background-color:skyblue;border-radius:10;");
    btn2->setEnabled(false);      //设置取消按钮不可用

    connect(this->btn1,&QPushButton::clicked,[&]()
    {
        alarmTime=lint1->text();
        alarmText=lint2->text();
        alarmSet=true;

        lint1->setEnabled(false);
        lint2->setEnabled(false);
        btn1->setEnabled(false);
        btn2->setEnabled(true);
    });
    connect(this->btn2,&QPushButton::clicked,[&]()
    {
        alarmSet=false;
        lint1->setEnabled(true);
        lint2->setEnabled(true);
        btn1->setEnabled(true);
        btn2->setEnabled(false);

    });
}

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

}



void Widget::mousePressEvent(QMouseEvent *event)
{
    temp = event->globalPos()-this->pos(); //求中间辅助向量

    if(event->button() == Qt::RightButton)
       {
           this->close();
       }
}

widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include        //文本转语音类

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void mousePressEvent(QMouseEvent *event) override;   //鼠标按下事件处理函数版
    void mouseMoveEvent(QMouseEvent *event) override; //鼠标移动事件处理函数
signals:




private:
    QTimer *timer;           //定义定时器变量
    QPoint temp;
    QTextToSpeech *speecher;     //定义播报员指针


    QLabel *lab1;  //显示系统时间
    QLabel *lab2;  //用于放背景图
    QLineEdit *lint1; //一号编辑框用于设置闹钟时间
    QLineEdit *lint2; //二号编辑框用于设置语音播报文本
    QPushButton *btn1; //启动按钮
    QPushButton *btn2; //取消按钮
    bool alarmSet=false;
    QString alarmTime;
    QString alarmText;


};
#endif // WIDGET_H

QT DAY4_第1张图片

思维导图QT DAY4_第2张图片

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