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

思维导图

相关推荐
hongmai6668883 分钟前
MP2348GTL-Z:这颗24V/4A同步降压芯片,把性能和体积平衡得恰到好处
c语言·开发语言·arm开发·单片机·5g
多弗朗皮卡丘6 分钟前
C语言梦开始的地方19:文件操作
c语言·开发语言
Dovis(誓平步青云)7 分钟前
折叠屏悬停看视频,上半屏和下半屏应该各做什么
android·java·服务器·开发语言·安全·音视频
benchmark_cc18 分钟前
Python量化实战:如何检测并剔除历史数据中的“闪崩/乌龙指”异常价格点?
开发语言·人工智能·python·量化·quantdash·量化数据源
2501_9378609421 分钟前
Java多线程初阶(下)—— synchronized、volatile、wait/notify与经典并发工具
java·开发语言·jvm
笨笨饿39 分钟前
#135_代码中的类型重定义艺术:从基础封装到函数类型设计
开发语言·stm32·单片机·嵌入式硬件·mcu·物联网·嵌入式实时数据库
统计学小王子42 分钟前
R语言入门——ggplot2图例增加公式
开发语言·r语言
牛油果子哥q43 分钟前
C++内存池与对象池精讲:内存碎片、自定义分配器、对象池实现、STL allocator原理、工程落地与性能对比
java·开发语言·c++
CodeStats1 小时前
《源纹天书》第三百六十三章至第三百六十四章
java·开发语言·源纹天书
Java后端的Ai之路1 小时前
03、Python - 抽象工厂模式
开发语言·python·设计模式·抽象工厂模式·通用智慧