2022.11.17work

实现闹钟功能

要求:在没有点击启动按钮前,系统时间正常显示,关闭按钮禁用状态,设置时间和文本编辑器为可用状态
在点击启动按钮后,启动按钮和设置时间以及文本编辑器为不可用状态,关闭按钮为可用状态
当点击关闭按钮时,关闭按钮不可用,设置时间、启动按钮、文本编辑器为可用状态

work1.h

#ifndef WORK1_H
#define WORK1_H

#include <QWidget>
#include <QDebug>
#include <QTime>
#include <QTimer>
#include <QTimerEvent>
#include <QTextToSpeech>
#include <QRegularExpression>
#include <QRegularExpressionMatch>


QT_BEGIN_NAMESPACE
namespace Ui { class work1; }
QT_END_NAMESPACE

class work1 : public QWidget
{
    Q_OBJECT

public:
    work1(QWidget *parent = nullptr);
    ~work1();

    QTimer *timer;

    void on_timer_init();
    void on_timer_ovetime();
    void show_label_time();
    void read_testedit();

private slots:
    void on_startbtn_clicked();


    void on_stopbtn_clicked();

private:
    Ui::work1 *ui;
    QTime curtime;
    QTime *clotime;
};
#endif // WORK1_H

work1.cpp

#include "work1.h"
#include "ui_work1.h"

work1::work1(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::work1)
{
    ui->setupUi(this);
    timer = new QTimer(this);

    // 计算器初始化
    connect(timer,&QTimer::timeout,this,&work1::on_timer_init);
    show_label_time();
    timer->start(10);

    // 设置显示字体
    ui->timelabel->setFont(QFont("宋体",40));
    ui->lineEdit->setFont(QFont("宋体",20));

    // 禁用关闭按钮
    ui->stopbtn->setDisabled(true);
}

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

// 计时器初始化
void work1::on_timer_init()
{
    if(curtime != QTime::currentTime()){
        disconnect(timer,&QTimer::timeout,this,&work1::on_timer_init);
    }else{
        return ;
    }
    timer->stop();
    connect(timer,&QTimer::timeout,this,&work1::on_timer_ovetime);
    timer->start(1000);
}

// 秒计时
void work1::on_timer_ovetime()
{
    show_label_time();
    if(curtime.hour()==clotime->hour() && clotime->minute()==curtime.minute() &&curtime.second()==0){
        read_testedit();
    }
}

// 时间显示
void work1::show_label_time()
{
    curtime = QTime::currentTime();
    ui->timelabel->setText(curtime.toString("hh:mm:ss"));
}

// 读文本内容
void work1::read_testedit()
{

    QTextToSpeech *speaker = new QTextToSpeech(this);
    speaker->setLocale(QLocale::Chinese);
    speaker->say(ui->textEdit->toPlainText());

}


void work1::on_startbtn_clicked()
{
    QRegularExpression reg("^(\\d\\d).(\\d\\d)$");
    QRegularExpressionMatch match = reg.match(ui->lineEdit->text());
//    qDebug()<<match.captured(1).toUInt()<<":"<<match.captured(2).toUInt();
    clotime = new QTime(match.captured(1).toUInt(),match.captured(2).toUInt());

    // 设置禁用
    ui->startbtn->setDisabled(true);
    ui->textEdit->setDisabled(true);
    ui->lineEdit->setDisabled(true);
    ui->stopbtn->setDisabled(false);
}

void work1::on_stopbtn_clicked()
{
    // 解除禁用
    ui->startbtn->setDisabled(false);
    ui->textEdit->setDisabled(false);
    ui->lineEdit->setDisabled(false);
    ui->stopbtn->setDisabled(true);
    ui->lineEdit->setText("");
    clotime->setHMS(-1,-1,-1);
}

work1.ui

运行效果

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值