QT绘制时钟

54 阅读1分钟

clock.png

1.panelpara.h

#ifndef PANELPARA_H
#define PANELPARA_H
#include <Qt>
#include <QColor>

class PanelPara
{
public:
    PanelPara(QColor c,int r);
    ~PanelPara();
    QColor color;
    int radius;
};

#endif // PANELPARA_H

2.panelpara.cpp

#include "panelpara.h"
#include <QDebug>

PanelPara::PanelPara(QColor c,int r): color(c),radius(r)
{
    qDebug()<<"init";
}

PanelPara::~PanelPara(){
     qDebug()<<"dest";
}

3.imagelable.h

#ifndef IMAGELABLE_H
#define IMAGELABLE_H
#define PANEL_RADIUS_NUM   3 //表盘背景图层数量
#define PANEL_RADIUS3  80    //表盘内圆最大半径
#define HOUR_NUM_SIZE 10     //时刻文字大小
#include <QWidget>
#include <QLabel>
#include <Qt>
#include <QColor>
#include <QPoint>
#include "panelpara.h"



class ImageLable : public QLabel
{
    Q_OBJECT
public:

    const QPoint hourHand[4] = {
        QPoint(3, 5),
        QPoint(0, 13),
        QPoint(-3, 5),
        QPoint(0, -PANEL_RADIUS3/3)
    };
    const QPoint minuteHand[4] = {
        QPoint(3, 5),
        QPoint(0, 16),
        QPoint(-3, 5),
        QPoint(0, -PANEL_RADIUS3*3/4)
    };
    const QPoint secondHand[4] = {
        QPoint(3, 5),
        QPoint(0, 18),
        QPoint(-3, 5),
        QPoint(0, -PANEL_RADIUS3)
    };
    const QColor hourColor = QColor(127, 0, 127);
    const QColor minuteColor = QColor(0, 127, 127);
    const QColor secondColor = QColor(127, 127, 0);

    explicit ImageLable(QWidget *parent = nullptr);
    ~ImageLable();
    void paintEvent(QPaintEvent *);

    PanelPara stPanelParaArr[PANEL_RADIUS_NUM] = {PanelPara(QColor(127, 0, 127),PANEL_RADIUS3+10),PanelPara(QColor(0, 127, 127),PANEL_RADIUS3+5),PanelPara(QColor(255, 255, 255),PANEL_RADIUS3)};


signals:

public slots:
};

#endif // IMAGELABLE_H

4.imagelable.cpp

#include "imagelable.h"
#include <QLabel>
#include <QDebug>
#include <QPainter>
#include <QPoint>
#include <QRectF>
#include <QTimer>
#include <QTime>
#include <QBrush>
#include <QFont>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

pid_t gettid()
{
return syscall(SYS_gettid);
}

void *call(void* arg){

    for(;;){
        qDebug()<<"call"<<getpid() << gettid();
        sleep(3);
    }

}

ImageLable::ImageLable(QWidget *parent) : QLabel(parent)
{
    QTimer *timer = new QTimer(this);
//    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    connect(timer, &QTimer::timeout, this, [=](){
        update();
    });
    timer->start(1000);

    setWindowTitle(tr("Clock"));
    setMinimumSize(200, 200); //设置最小尺寸

//    pthread_t tid;
//    pthread_create(&tid,NULL,call,(void*)this);
}

ImageLable::~ImageLable(){
    qDebug()<<"img des";
}

void ImageLable::paintEvent(QPaintEvent *e){

         int side = qMin(width(), height());
         QTime time = QTime::currentTime();
         QPainter painter(this);
         painter.setRenderHint(QPainter::Antialiasing);
         painter.translate(width()/2, height()/2); //画图的基准位置
         painter.scale(side/200.0, side/200.0); //随窗口尺寸自动缩放
         //表盘背景图层
         for (int i=0; i<PANEL_RADIUS_NUM; i++)
         {
             QBrush brush(stPanelParaArr[i].color);
             QPen pen(stPanelParaArr[i].color);
             painter.setBrush(brush);
             painter.setPen(pen);
             painter.drawEllipse(-stPanelParaArr[i].radius, -stPanelParaArr[i].radius, 2*stPanelParaArr[i].radius, 2*stPanelParaArr[i].radius);
         }

         //数字时钟
         painter.setPen(hourColor);
         QString text  = time.toString();
         QRect r = painter.boundingRect( QRect(), 0, text );
         painter.drawText(0-r.width()/2,0+20+r.height()/2, text);

         //时针
         painter.setPen(Qt::NoPen);
         painter.setBrush(hourColor);

         painter.save();
         painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
         painter.drawConvexPolygon(hourHand, 4);
         painter.restore();

         //小时的刻度
         painter.setPen(hourColor);
         for (int i = 0; i < 12; ++i)
         {
             painter.rotate(30.0);
             painter.drawLine(PANEL_RADIUS3-6, 0, PANEL_RADIUS3, 0);
             QFont font("TimesNewRoman", HOUR_NUM_SIZE);
             painter.setFont(font);
             painter.drawText(-HOUR_NUM_SIZE, -(PANEL_RADIUS3-10), 2*HOUR_NUM_SIZE, 2*HOUR_NUM_SIZE, Qt::AlignHCenter, QString::number(i+1));
         }

         //分针
         painter.setPen(Qt::NoPen);
         painter.setBrush(minuteColor);

         painter.save();
         painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
         painter.drawConvexPolygon(minuteHand, 4);
         painter.restore();

         painter.setPen(minuteColor);
         for (int j = 0; j < 60; ++j)
         {
             if ((j % 5) != 0)
             {
                 painter.drawLine(PANEL_RADIUS3-4, 0, PANEL_RADIUS3, 0);
             }
             painter.rotate(6.0);
         }

         //秒针
         painter.setPen(Qt::NoPen);
         painter.setBrush(secondColor);

         painter.save();
         painter.rotate(6.0 * time.second());
         painter.drawConvexPolygon(secondHand, 4);
         painter.restore();

         painter.end();

}