day4_QT

day4_QT

  • qt绘制钟表

qt绘制钟表

day4_QT_第1张图片

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

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

    this->resize(1000,1000);
    this->setStyleSheet("background-color:white;");

    //启动计时器
    time_id = startTimer(1000);

}


void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this); //定义一个画家
    QPen pen;
    pen.setColor(QColor("pink"));//画笔颜色
    pen.setStyle(Qt::SolidLine);//画笔风格 实线
    pen.setWidth(5);

    painter.setPen(pen);        //将画笔赋予画家实例

    //将中心点转移
    painter.translate(this->width()/2,this->height()/2);

    painter.drawEllipse(QPoint(0,0),360,360);   //360是半径
    /*绘制 12个时 60个分*/
    pen.setColor(QColor("red"));
    painter.setPen(pen);
    //十二等分
    for(int i = 0;i<12;i++)
    {
        painter.drawLine(0,-300, 0, -355);
        painter.rotate(30);
    }
    pen.setColor(QColor("black"));
    painter.setPen(pen);
    //六十等分
    for(int i = 0;i<60;i++)
    {
        if(i%5 != 0)
        {
            painter.drawLine(0,-330, 0, -355);
        }
        painter.rotate(6);
    }

    painter.rotate(30);
    painter.setFont(QFont("Helvetica [Cronyx]", 25, 6));
    for(int i = 1;i<=12;i++)
    {
        painter.drawText(0, -270,QString::number(i,10));
        painter.rotate(30);
    }



    //秒
    pen.setColor(QColor("blue"));
    pen.setWidth(4);
    painter.setPen(pen);
    painter.rotate(sec*6);
    painter.drawLine(0,0, 0, -275);

    //分
    painter.rotate(-(sec*6));
    pen.setColor(QColor("pink"));
    pen.setWidth(6);
    painter.setPen(pen);
    painter.rotate((min-1)*6);
    painter.drawLine(0,0, 0, -200);

    //时
    painter.rotate(-(min*6));
    pen.setColor(QColor("red"));
    pen.setWidth(10);
    painter.setPen(pen);
    painter.rotate(hour*30);
    painter.drawLine(0,0, 0, -150);


}

void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == time_id)
    {
        QTime sys_t = QTime::currentTime();
        hour = sys_t.hour();
        min = sys_t.minute();
        sec = sys_t.second();
        //qDebug()<killTimer(time_id);
    delete ui;
}

你可能感兴趣的:(learn,qt,开发语言)