欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用QT实现时钟

程序员文章站 2022-05-08 17:04:59
...

原理

QPainter 提供了绘图的常用操作,QTimer 提供了定时器功能,两者结合,可以绘制出各种不同风格的时钟。

实现方式

绘制时钟,包括时针、分针、秒针、刻度等。

void AnalogClock::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    //表针初始位置
    static  QPoint hourHand[3] = 
    {
        QPoint(8, 8),
        QPoint(-8, 8),
        QPoint(0, -40)
    };
    static  QPoint minuteHand[3] = 
    {
        QPoint(6, 9),
        QPoint(-6, 9),
        QPoint(0, -70)
    };
    static  QPoint secondHand[3] = 
    {
        QPoint(5, 10),
        QPoint(-5, 10),
        QPoint(0, -90)
    };
    QColor hourColor(225, 255, 255);
    QColor minuteColor(176, 224, 230, 191);
    QColor secondColor(0,206,209);



    int side = qMin(width(), height());
    QTime time = QTime::currentTime();
    QPainter painter(this);



    QPixmap pixmap(":/5.jpg");//背景图
    painter.drawPixmap(0,0,width(),height(),pixmap);

    //表盘
    painter.setRenderHint(QPainter::Antialiasing);//去锯齿
    painter.translate(width()/2, height()/2);
    painter.scale(side /250.0, side /250.0);
  
    //时针
    painter.setPen(Qt::NoPen);
    painter.setBrush(hourColor);
    painter.save();
    painter.rotate(30.0 * (time.hour() + time.minute() / 60.0));
    painter.drawConvexPolygon(hourHand, 3);
    painter.restore();


    //分针
    painter.setPen(Qt::NoPen);
    painter.setBrush(minuteColor);
    painter.save();
    painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
    painter.drawConvexPolygon(minuteHand, 3);
    painter.restore();


    //秒针
    painter.setPen(Qt::NoPen);
    painter.setBrush(secondColor);
    painter.save();
    painter.rotate(6.0 * time.second());
    painter.drawConvexPolygon(secondHand, 3);
    painter.restore();

     //画小时文本
    painter.setPen(hourColor);
    int r=108;
    QFont font=painter.font();
    int pointSize=font.pointSize();
    for(int i=0;i<12;i++)
    {
        int num;
        if(i==0)
            num=12;
        else
            num=i;
         painter.drawText(textRectF(r,pointSize,i*30-90),Qt::AlignCenter,QString::number(num));
    }

    //画表盘时线
    painter.setPen(hourColor);
    for (int i = 0; i <12; i++)
    {
        painter.drawLine(0, 88, 0, 96);
        painter.rotate(30.0);
    }


    //画钟盘分线
    painter.setPen(Qt::NoPen);
    painter.setPen(minuteColor);
    for (int j = 0; j < 60; ++j) 
    {
        if ((j % 5) != 0)
            painter.drawLine(92, 0, 96, 0);
        painter.rotate(6.0);
    }


}

绘制小时对应的数字,通过 textRectF() 实现

//绘制文本区域
QRectF AnalogClock::textRectF(double r, int pointSize, double angle)
{
    QRectF rectf;
    //pointsize字体大小
    rectf.setX(r*qCos(angle*M_PI/180.0)-pointSize*2);
    rectf.setY(r*qSin(angle*M_PI/180.0)-pointSize);
    rectf.setWidth(pointSize*4);
    rectf.setHeight(pointSize*2);
    return rectf;
}

analogclock.h

#ifndef ANALOGCLOCK_H
#define ANALOGCLOCK_H

#include <QWidget>
#include<QPoint>
#include<QPainter>


class AnalogClock : public QWidget
{
    Q_OBJECT

public:
    AnalogClock(QWidget *parent = nullptr);
    ~AnalogClock();
  
    void paintEvent(QPaintEvent *event);
    QRectF textRectF(double r,int pointSize,double angle);
private:
    QPoint m_point;
    Ui::AnalogClock *ui;
};
#endif // ANALOGCLOCK_H

实现转动

首先,我们构造一个定时器 QTimer,连接其超时信号 timeout() 到槽函数 update(),当调用 update() 时,系统会自动通知当前界面进行重绘paintEvent()。

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

    QTimer *timer = new QTimer(this);
  
        connect(timer, SIGNAL(timeout()), this, SLOT(update()));
              timer->start(1000)}

利用QT实现时钟