用Qt完成刻度时钟

作业:用Qt完成刻度时钟

功能代码如下:

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //启动定时器
    timer_1 = startTimer(1000); //功能:启动一个定时器,参数:毫秒。
                                //每隔1000毫秒,会自动调用timerEvent函数
                                //返回值:启动的一个定时器的id号
}

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

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter p1(this);

    //给画家准备画笔
    p1.setPen(Qt::blue);
    p1.setFont(QFont("黑体",10));

    QBrush b1(QColor("pink"));
    p1.setBrush(b1);  //设置刷子

    //移动画家的起始坐标
    p1.translate(this->width()/2,this->height()/2);

    //旋转坐标
    //p1.rotate(90);

    //画椭圆
    p1.drawEllipse(QPoint(0,0),150,150);


    for(int i=0;i<60;i++)
    {
        QPen pen;
        //p.setPen(QColor("green"));
        pen.setColor(QColor("black"));
        pen.setWidth(2);
        p1.setPen(pen);
        p1.drawLine(QPoint(0,150),QPoint(0,145));  //画线
        p1.rotate(6);
    }
    for(int i=0;i<12;i++)
    {
        QPen pen;
        pen.setColor(QColor("black"));
        pen.setWidth(5);
        p1.setPen(pen);
        p1.drawLine(QPoint(0,150),QPoint(0,143));    //画线
        p1.rotate(30);
    }


    //画时针
    QPen pen1;
    pen1.setColor(QColor("red"));
    pen1.setWidth(5);
    p1.setPen(pen1);
    p1.rotate(hour%12*30+(minute*60+second)/720*6);
    p1.drawLine(QPoint(0,0),QPoint(0,-50));

    QPainter p2(this);

    //给画家准备画笔
    p2.setPen(Qt::blue);
    p2.setFont(QFont("黑体",10));

    QBrush b2(QColor("pink"));
    p2.setBrush(b2);  //设置刷子

    //移动画家的起始坐标
    p2.translate(this->width()/2,this->height()/2);

    //画分针
    QPen pen2;
    pen2.setColor(QColor("blue"));
    pen2.setWidth(3);
    p2.setPen(pen2);
    p2.rotate(minute*6+second/60*6);
    p2.drawLine(QPoint(0,0),QPoint(0,-120));

    QPainter p3(this);

    //给画家准备画笔
    p3.setPen(Qt::blue);
    p3.setFont(QFont("黑体",10));

    QBrush b3(QColor("pink"));
    p3.setBrush(b3);  //设置刷子

    //移动画家的起始坐标
    p3.translate(this->width()/2,this->height()/2);

    //画秒针
    QPen pen3;
    pen3.setColor(QColor("red"));
    pen3.setWidth(1);
    p3.setPen(pen3);
    p3.rotate(second*6);
    qDebug()<<"second = "<<second*6;
    p3.drawLine(QPoint(0,0),QPoint(0,-150));


    //QPen pen;


}

//重写的定时器事件处理函数
void Widget::timerEvent(QTimerEvent *event)
{

        //获取时间
        QTime time = QTime::currentTime();

        //将时间转换成字符串
        QString t = time.toString("HH:mm:ss");

        //将时间展示到ui界面上
        //ui->label1->setText(t);

        QStringList timeList = t.split(':');
        //qDebug()<<timeList.size();
        for(int i=0;i<timeList.size();i++)
        {
            qDebug()<<timeList[i];
        }

        hour = timeList[0].toUInt(); //将字符串转变成无符号整数
        minute = timeList[1].toUInt();
        second = timeList[2].toUInt();
        qDebug()<<hour<<":"<<minute<<":"<<second;


        update();
        qDebug()<<time;
}

代码演示结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大鱼YY

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

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

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

打赏作者

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

抵扣说明:

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

余额充值