# 2023/4/8 时钟练习

2023/4/8 时钟练习

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


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

    //实例化一个播报员
    broadCaster = new QTextToSpeech(this);
    get_time();

    initCurrentTime();

    connect(t,&QTimer::timeout,this,&Widget::updateClock);


}

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



//在currenTime中显示当前日期时间
void Widget::initCurrentTime()
{
    connect(t,&QTimer::timeout,this,&Widget::showCurrentTime);
    t->start(1000);

    showCurrentTime();
}

void Widget::showCurrentTime()
{

    //获取系统日期时间    //将日期时间转为字符串
    QString date = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    //将字符串显示在ui界面
    ui->current_Time->setText(date);
    //居中显示
    ui->current_Time->setAlignment(Qt::AlignCenter);
}

void Widget::get_time()
{
    //获取当前时间
    QString curr_time = QTime::currentTime().toString("hh:mm:ss");
    qDebug()<<curr_time;

    QStringList current_List = curr_time.split(":");

    hour_A = current_List[0].toUInt();
    min_A = current_List[1].toUInt();
    sec_A = current_List[2].toUInt();
    qDebug()<<hour_A << " " << min_A << " " << sec_A;

}



//非事件处理函数版对应的槽函数
void Widget::on_startBtn_clicked()
{
    if(ui->startBtn->text() == "启动")
    {
        t_id = this->startTimer(1000);
        ui->startBtn->setDisabled(true);  //点击后锁定启动按钮
        ui->stopBtn->setDisabled(false);  //点击后解锁关闭按钮
    }
}

void Widget::on_stopBtn_clicked()
{
    if(ui->stopBtn->text() == "关闭")
    {
        if(t_id != 0)
        {
            this->killTimer(t_id);
        }
        ui->startBtn->setDisabled(false);//点击后锁定关闭按钮
        ui->stopBtn->setDisabled(true);  //点击后解锁启动按钮
    }
}


//重写定时器事件处理函数
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == t_id)
    {
        //获取系统时间,将时间转为字符串
        QString time = QTime::currentTime().toString("hh:mm:ss");
        QStringList list = time.split(":");
        int hh,mm,ss;
        hh = list[0].toUInt();
        mm = list[1].toUInt();
        ss = list[2].toUInt();

        //获取闹钟定下的时间
        int h,m,s;
        QStringList list_clock = ui->set_alarm->text().split(":");
        h = list_clock[0].toUInt();
        m = list_clock[1].toUInt();
        s = list_clock[2].toUInt();

        qDebug()<< h << " " << m << "  " << s;
        //比较闹钟时间与当前时间

        if(h == hh && m == mm && s == ss)
        {
            //播报语音
            broadCaster->say(ui->broadcast->toPlainText());
            qDebug()<<"成功";
            this->killTimer(t_id);
            ui->startBtn->setDisabled(false);//点击后锁定关闭按钮
            ui->stopBtn->setDisabled(true);  //点击后解锁启动按钮
        }
    }
}

//重写鼠标按压事件
void Widget::mousePressEvent(QMouseEvent *event)
{
    startPoint = event->globalPos() - this->frameGeometry().topLeft();
}

//重写鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - startPoint);
}


//重写绘画处理函数
void Widget::paintEvent(QPaintEvent *event)
{
    //实例化一个画家类
    QPainter p(this);

    //设置绘画颜色
    p.setPen(QColor(85,170,255));

    //设置绘画字体
    p.setFont(QFont("宋体"));

    //设置绘画起点
    p.translate(this->width()/4,this->height()/2+30);

    //画圆
    p.drawEllipse(QPointF(0,0),100,100);

    //绘制时刻表
    p.setPen(QColor(255,0,255));
    for(int i=1;i<=60;i++)
    {
        QPen pen;
        p.rotate(6);
        if(i%5==0)
        {
            pen.setWidth(4);
            p.drawLine(0,-100,0,-80);
            p.drawText(0,-80,QString("%1").arg(i/5));
        }else
        {
            pen.setWidth(1);
            p.drawLine(0,-100,0,-95);
        }
    }

    p.setPen(QPen(Qt::red,2));
    p.save();
    p.rotate(sec_Angle);
    p.drawLine(0,-70,0,0);//秒针
    p.restore();

    p.setPen(QPen(Qt::blue,4));
    p.save();
    p.rotate(min_Angle);
    p.drawLine(0,-50,0,0);//分针
    p.restore();

    p.setPen(QPen(Qt::green,6));
    p.save();
    p.rotate(hour_Angle);
    p.drawLine(0,-30,0,0);//时针
    p.restore();

}


void Widget::updateClock()
{
    count ++;
    sec_Angle = 6.0*sec_A + 6.0*count;
    min_Angle = 6.0*min_A + sec_A*6.0/60 + count*6.0/60;
    hour_Angle = 30.0*hour_A + min_A*6.0/12 + sec_A*6.0/60/12 + count*6.0/60/12;
    update();

}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * File: ADC.c * Author: tlfte *AD转换,结果在C口和D口的LED上显示,能进行各种通道选择和参考电压,结果对齐方式选择 * Created on 2018年8月6日, 上午10:07 练习AD结果的计算验证,AD_RESULT=VIN×1023÷VREF,讲解887头文件的作用 */ // PIC16F887 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD) #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled) #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled) #pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled) #pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled) #pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming) // CONFIG2 #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V) #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include <xc.h> #define _XTAL_FREQ 4000000 //指明时钟晶振为4MHz,使delay宏定义可以正常使用 void CSH(void); unsigned int AD_SUB(char k); void main( ) { unsigned int y; CSH(); while(1) { __delay_ms(100); //每隔100毫秒循环一次 y=AD_
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值