QTday4

作业

实现闹钟功能

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDateTime>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QTimerEvent>
#include <QtTextToSpeech>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    Ui::Widget *ui;
    QLabel *time_lab;
    QPushButton *start_btn;
    QPushButton *stop_btn;
    QPushButton *cancel_btn;
    QLineEdit *settime_line;
    QTextEdit *text;
    QTextToSpeech *speecher;
    int timer_id1;
    int timer_id2;
public slots:
    void slot_cancel();
    void slot_start();
    void slot_stop();
};
#endif // WIDGET_H

 源文件

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //播报员
    speecher = new QTextToSpeech(this);
    //主界面
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setFixedSize(630,520);
    //时间
    time_lab = new QLabel(this);
    time_lab->resize(400,100);
    time_lab->setStyleSheet("QLabel{background-color:rgb(0,255,255)}");
    //按钮
    start_btn = new QPushButton(this);
    stop_btn = new QPushButton(this);
    start_btn->setText("启动");
    start_btn->resize(100,50);
    start_btn->move(time_lab->x()+time_lab->width()+20,time_lab->y()+50);
    stop_btn->resize(start_btn->size());
    stop_btn->setText("停止");
    stop_btn->move(start_btn->x()+start_btn->width()+10,start_btn->y());
    stop_btn->setEnabled(false);
    connect(start_btn,&QPushButton::clicked,this,&Widget::slot_start);
    connect(stop_btn,&QPushButton::clicked,this,&Widget::slot_stop);
    //定时框
    settime_line = new QLineEdit(this);
    settime_line->resize(210,50);
    settime_line->clear();
    settime_line->move(start_btn->x(),time_lab->y());
    //提醒
    text = new QTextEdit(this);
    text->resize(630,400);
    text->move(time_lab->x(),time_lab->y()+time_lab->height()+20);
    text->setFont(QFont("微软雅黑",18));
    cancel_btn =new QPushButton(this);    cancel_btn->setText("取消");
    cancel_btn->resize(start_btn->size());
    connect(this->cancel_btn,&QPushButton::clicked,this,&Widget::slot_cancel);
    cancel_btn->move(this->width()-100,this->height()-50);
    //时钟
    timer_id1 = this->startTimer(1000);
}

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

void Widget::slot_cancel()
{
    this->close();
}

void Widget::slot_start()
{
    this->stop_btn->setEnabled(true);
    this->start_btn->setEnabled(false);
    this->settime_line->setEnabled(false);
    this->text->setEnabled(false);
    timer_id2 = this->startTimer(1000);
}

void Widget::slot_stop()
{
    this->stop_btn->setEnabled(false);
    this->start_btn->setEnabled(true);
    this->settime_line->setEnabled(true);
    this->text->setEnabled(true);
    this->text->clear();
    this->settime_line->clear();
    this->killTimer(timer_id2);
}

void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == timer_id1)
    {
        QDateTime dt = QDateTime::currentDateTime();
        QString s1 = dt.toString("yyyy:MM:dd hh:mm:ss");
        this->time_lab->setText(s1);
        this->time_lab->setAlignment(Qt::AlignHCenter);
        this->time_lab->setFont(QFont("微软雅黑",20));
    }else if(event->timerId() == timer_id2)
    {
        QString buf = settime_line->text();
        QDateTime dt1 = QDateTime::currentDateTime();
        QString s2 = dt1.toString("hh:mm:ss");
        if(buf == s2)
        {
            speecher->say(this->text->toPlainText());
        }
    }
}

思维导图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值