day47

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    t=new QTimer(this);
    time=startTimer(1000);
    QTime nowtime = QTime::currentTime();
    h=nowtime.hour();
    m=nowtime.minute();
    s=nowtime.second();
}
Widget::~Widget()
{
    delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
    //实例化画家
    QPoint center(this->width()/2,this->height()/2);
    QPainter painter(this);
    //实例化的画笔
    QPen pen;
    pen.setColor(QColor("red"));
    pen.setStyle(Qt::SolidLine);
    painter.translate(center);
    pen.setWidth(6);
    painter.setPen(pen);
    painter.setBrush(QBrush(QColor("pink")));
    painter.drawEllipse(QPoint(0,0),200,200);
    for(int i=0;i<12;i++)
    {
        painter.rotate(30);
        painter.drawLine(0,-180,0,-200);
    }
    for(int i=0;i<60;i++)
    {
        painter.rotate(6);
        painter.drawLine(0,-190,0,-200);
    }
    painter.end();

    QPainter painters(this);
    pen.setColor(QColor("blue"));
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(5);
    painters.setPen(pen);
    painters.translate(center);
    painters.rotate(h*30+m*0.5);
    painters.drawLine(0,0,0,-80);
    painters.end();

    QPainter paintem(this);
    pen.setColor(QColor("green"));
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(5);
    paintem.setPen(pen);
    paintem.translate(center);
    paintem.rotate(m*6);
    paintem.drawLine(0,0,0,-120);
    paintem.end();

    QPainter paintems(this);
    pen.setColor(QColor("black"));
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(5);
    paintems.setPen(pen);
    paintems.translate(center);
    paintems.rotate(s*6);
    paintems.drawLine(0,0,0,-150);
    paintems.end();

    update();
}
void Widget::timerEvent(QTimerEvent *event)
{
    s++;
    if(s==60)
    {
        s=0;
        m++;
        if(m==60)
        {
            m=0;
            h++;
            if(h==24)
            {
                h=0;
            }
        }
    }
}

day47_第1张图片

你可能感兴趣的:(qt)