QT作业(闹钟)(0902-林雪阵)

 .h

#ifndef MYCLOCK_H
#define MYCLOCK_H

#include <QWidget>
#include<QLCDNumber>
#include<QTime>
#include <QPushButton>
#include <QTextToSpeech>

QT_BEGIN_NAMESPACE
namespace Ui { class MyClock; }
QT_END_NAMESPACE

class MyClock : public QWidget
{
    Q_OBJECT
signals:
    void singal_1();

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

private slots:
    void on_startBtn_clicked();

    void on_StopBtn_clicked();

    void timerEvent(QTimerEvent* e);  //重写定时器事件

    void on_hourBtn_clicked();

    void on_minBtn_clicked();

    void sayDeal();


private:
    Ui::MyClock *ui;

    //定义闪烁秒状态
    int showFlag;
    QString clockTime;
    int clockFlag;
    QTextToSpeech speech;

    int sayflag=1;


};
#endif // MYCLOCK_H

.cpp

#include "myclock.h"
#include "ui_myclock.h"
#include<string>
#include<windows.h>
MyClock::MyClock(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyClock)
{
    ui->setupUi(this);
    startTimer(1000);
    ui->Lcd2->display("00:00");
    clockTime = "00:00";
    //speech.setRate(3);
    connect(this, &MyClock::singal_1, this, &MyClock::sayDeal);
}

MyClock::~MyClock()
{
    delete ui;

}

void MyClock::on_startBtn_clicked()
{
    clockFlag=true;
}

void MyClock::on_StopBtn_clicked()
{
    clockFlag=false;
}

void MyClock::timerEvent(QTimerEvent *)
{
    //1.获取系统时间
    QTime sysTime = QTime::currentTime();

    //将系统时间转换为字符串
    QString timeText = sysTime.toString("hh:mm");

    if(showFlag)
    {
        timeText[2]=':';
        clockTime[2]=':';
        showFlag = false;
    }else
    {

        timeText[2]=' ';
       // clockTime[2]=' ';
        showFlag = true;
    }
    //将时间字符串展示到LCD中
    ui->Lcd1->display(timeText);
    ui->Lcd2->display(clockTime);

    if(clockFlag)
    {
        if(timeText==clockTime && sayflag==1)
        {
            emit singal_1();
        }
    }

}

void MyClock::on_hourBtn_clicked()
{
    QString hour;
    for(int i=0;i<2;i++)
    {
        hour[i]=clockTime[i];
    }
    int hh=hour.toInt();
    hh++;
    if(hh==24)
        hh=0;
    if(hh<10)
    {
        hour="0"+QString::number(hh);
    }else{
        hour=QString::number(hh);
    }
    for(int i=0;i<2;i++)
    {
        clockTime[i]= hour[i];
    }

    ui->Lcd2->display(clockTime);

}

void MyClock::on_minBtn_clicked()
{
    QString mint;
    for(int i=0;i<2;i++)
    {
        mint[i]=clockTime[3+i];
    }
    int mm=mint.toInt();
    mm++;
    if(mm==60)
        mm=0;
    if(mm<10)
    {
        mint="0"+QString::number(mm);
    }else{
        mint=QString::number(mm);
    }
    for(int i=0;i<2;i++)
    {
        clockTime[3+i]= mint[i];
    }

    ui->Lcd2->display(clockTime);
}

void MyClock::sayDeal()
{
    int len = ui->textEdit->toPlainText().size();
    int time =len/4;
    speech.say(ui->textEdit->toPlainText());
    Sleep(time*1000);
    sayflag=1;
}


.pro

QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    myclock.cpp

HEADERS += \
    myclock.h

FORMS += \
    myclock.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林某某..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值