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&&currentTime==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 <QApplication>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>
#include <QTime>
#include <QTimer>
#include <QMouseEvent>
#include <QDateTime>
#include <QTextToSpeech>       //文本转语音类

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

思维导图

相关推荐
学步_技术4 分钟前
Python编码系列—Python组合模式:构建灵活的对象组合
开发语言·python·组合模式
o独酌o28 分钟前
递归的‘浅’理解
java·开发语言
Book_熬夜!31 分钟前
Python基础(六)——PyEcharts数据可视化初级版
开发语言·python·信息可视化·echarts·数据可视化
m0_631270401 小时前
高级c语言(五)
c语言·开发语言
2401_858286111 小时前
53.【C语言】 字符函数和字符串函数(strcmp函数)
c语言·开发语言
程序猿练习生2 小时前
C++速通LeetCode中等第5题-无重复字符的最长字串
开发语言·c++·leetcode
2401_858120262 小时前
MATLAB中的无线通信系统部署和优化工具有哪些
开发语言·matlab
MATLAB滤波2 小时前
【PSINS】基于PSINS工具箱的EKF+UKF对比程序|三维定位|组合导航|MATLAB
开发语言·matlab
2401_858120532 小时前
MATLAB在嵌入式系统设计中的最佳实践
开发语言·matlab
蓝裕安2 小时前
伪工厂模式制造敌人
开发语言·unity·游戏引擎