Qt开源 自绘时钟

cpp 复制代码
#ifndef CLOCK1_H
#define CLOCK1_H

#include <QTime>
#include <QtMath>
#include <QDialog>
#include <QPainter>
#include <QPaintEvent>
#include <QPainterPath>

class Clock1 : public QDialog
{
    Q_OBJECT

private:
    double hour;
    double minute;
    double second;

public:
    Clock1(QWidget *parent = nullptr);
    ~Clock1();

protected:
    virtual void timerEvent(QTimerEvent*);
    virtual void paintEvent(QPaintEvent*);

public:
    void DrawScaleLine(QPainter&,int);
    void DrawDialNumber(QPainter&,int);
    void DrawHourPointer(QPainter&,int);
    void DrawSecondPointer(QPainter&,int);
    void DrawMinutePointer(QPainter&,int);
    void DrawCircularRing1(QPainter&,int,int);
    void DrawCircularRing2(QPainter&,int,int);
    void DrawCircularRing3(QPainter&,int,int);
};
#endif
cpp 复制代码
#include "Clock1.h"

Clock1::Clock1(QWidget *parent)
    : QDialog(parent)
{
    //初始化变量
    hour=0;
    minute=0;
    second=0;
    //设置窗口大小
    setFixedSize(600,600);
    //去掉问号
    Qt::WindowFlags flags= this->windowFlags();
    setWindowFlags(flags&~Qt::WindowContextHelpButtonHint);
    //背景设置成白色
    QPalette bgpal=palette();
    bgpal.setColor(QPalette::Background,QColor(255,255,255));
    setPalette(bgpal);
    //设置定时器
    startTimer(1000);
    //执行一次绘图
    QTime curr_time =QTime::currentTime();
    second=curr_time.second();
    minute=curr_time.minute()+second/60;
    hour=curr_time.hour()+minute/60;
}

Clock1::~Clock1(){}

#include <QDebug>

void Clock1::timerEvent(QTimerEvent*)
{
    QTime curr_time=QTime::currentTime();
    second=curr_time.second();
    minute=curr_time.minute()+second/60;
    hour=curr_time.hour()+minute/60;
    update();
}

void Clock1::paintEvent(QPaintEvent*)
{
    //初始化画图对象
    QPainter painter(this);
    //绘图坐标移动到中心
    int width=this->width();
    int height=this->height();
    painter.translate(width>>1,height>>1);
    int radius=((width>height)?height:width)/2-30;
    //启用反锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);
    //取消画笔
    painter.setPen(Qt::NoPen);
    //绘制第一层圆环
    DrawCircularRing1(painter,radius,radius-4);
    //绘制第二层圆环
    DrawCircularRing2(painter,radius-4,radius-20);
    //绘制第三层圆环
    DrawCircularRing3(painter,radius-20,radius-25);
    //绘制表盘刻度线
    DrawScaleLine(painter,radius-35);
    //绘制表盘上数字
    DrawDialNumber(painter,radius-70);
    //绘制时针
    DrawHourPointer(painter,radius-150);
    //绘制分针
    DrawMinutePointer(painter,radius-120);
    //绘制秒针
    DrawSecondPointer(painter,radius-100);
}

void Clock1::DrawCircularRing1(QPainter& painter,int radius1,int radius2)
{
    //保存绘图对象
    painter.save();
    //计算大小圆路径
    QPainterPath outRing;
    QPainterPath inRing;
    outRing.moveTo(0,0);
    inRing.moveTo(0,0);
    outRing.addEllipse(-radius1,-radius1, 2*radius1,2*radius1);
    inRing.addEllipse(-radius2,-radius2,2*radius2,2*radius2);
    outRing.closeSubpath();
    //设置画刷
    painter.setBrush(QColor(200,200,200));
    //大圆减去小圆得到圆环
    painter.drawPath(outRing.subtracted(inRing));
    //恢复绘图对象
    painter.restore();
}

void Clock1::DrawCircularRing2(QPainter& painter,int radius1,int radius2)
{
    //保存绘图对象
    painter.save();
    //计算大小圆路径
    QPainterPath outRing;
    QPainterPath inRing;
    outRing.moveTo(0,0);
    inRing.moveTo(0,0);
    outRing.addEllipse(-radius1,-radius1, 2*radius1,2*radius1);
    inRing.addEllipse(-radius2,-radius2,2*radius2,2*radius2);
    outRing.closeSubpath();
    //设置画刷
    painter.setBrush(QColor(235,235,235));
    //大圆减去小圆得到圆环
    painter.drawPath(outRing.subtracted(inRing));
    //恢复绘图对象
    painter.restore();
}

void Clock1::DrawCircularRing3(QPainter& painter,int radius1,int radius2)
{
    //保存绘图对象
    painter.save();
    //计算大小圆路径
    QPainterPath outRing;
    QPainterPath inRing;
    outRing.moveTo(0,0);
    inRing.moveTo(0,0);
    outRing.addEllipse(-radius1,-radius1, 2*radius1,2*radius1);
    inRing.addEllipse(-radius2,-radius2,2*radius2,2*radius2);
    outRing.closeSubpath();
    //设置画刷
    painter.setBrush(QColor(180,180,180));
    //大圆减去小圆得到圆环
    painter.drawPath(outRing.subtracted(inRing));
    //恢复绘图对象
    painter.restore();
}

void Clock1::DrawScaleLine(QPainter& painter,int radius)
{
    //设置画刷
    painter.setBrush(QColor(100,100,100));
    //组装点的路径图
    QPainterPath pointPath1;
    pointPath1.moveTo(-2,-2);
    pointPath1.lineTo(-1,-4);
    pointPath1.lineTo(1,-4);
    pointPath1.lineTo(2,-2);
    pointPath1.lineTo(1,8);
    pointPath1.lineTo(-1,8);
    QPainterPath pointPath2;
    pointPath2.moveTo(-2,-2);
    pointPath2.lineTo(-1,-4);
    pointPath2.lineTo(1,-4);
    pointPath2.lineTo(2,-2);
    pointPath2.lineTo(1,20);
    pointPath2.lineTo(-1,20);
    //绘制25个刻度
    for(int i=0;i<60;++i){
        QPointF point(0,0);
        painter.save();
        //计算并移动绘图对象中心点
        point.setX(radius*qCos(((90-i*6)*M_PI)/180));
        point.setY(radius*qSin(((90-i*6)*M_PI)/180));
        //计算并移动绘图对象的中心点
        painter.translate(point.x(),-point.y());
        //计算并选择绘图对象坐标
        painter.rotate(i*6);
        //绘制路径
        if(i%5){
            painter.drawPath(pointPath1);
        }
        else{
            painter.drawPath(pointPath2);
        }
        painter.restore();
    }
}

void Clock1::DrawDialNumber(QPainter& painter,int radius)
{
    painter.setPen(QColor(100,100,100));
    QFont font;
    font.setFamily("SimHei");
    font.setPointSize(16);
    painter.setFont(font);
    //绘制13个小点
    for(int i=0;i<12;++i){
        QPointF point(0,0);
        painter.save();
        //计算并移动绘图对象中心点
        point.setX(radius*qCos(((60-i*30)*M_PI)/180));
        point.setY(radius*qSin(((60-i*30)*M_PI)/180));
        //计算并移动绘图对象的中心点
        painter.translate(point.x(),-point.y());
        //绘制路径
        painter.drawText(-15, -15, 30, 30,Qt::AlignCenter,QString::number(i+1));
        painter.restore();
    }
}

void Clock1::DrawHourPointer(QPainter& painter,int radius)
{
    painter.setPen(Qt::NoPen);
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(10,0);
    pointPath.lineTo(1,-radius);
    pointPath.lineTo(-1,-radius);
    pointPath.lineTo(-10,0);
    pointPath.arcTo(-10,0,20,20,180,180);
    painter.save();
    //计算并选择绘图对象坐标
    painter.rotate(hour*30);
    //设置画刷
    painter.setBrush(QColor(0,0,0,200));
    //绘制路径
    painter.drawPath(pointPath);
    painter.restore();
}

void Clock1::DrawMinutePointer(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(10,0);
    pointPath.lineTo(1,-radius);
    pointPath.lineTo(-1,-radius);
    pointPath.lineTo(-10,0);
    pointPath.arcTo(-10,0,20,20,180,180);
    painter.save();
    //计算并选择绘图对象坐标
    painter.rotate(minute*6);
    //设置画刷
    painter.setBrush(QColor(0,0,0,200));
    //绘制路径
    painter.drawPath(pointPath);
    painter.restore();
}

void Clock1::DrawSecondPointer(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(10,0);
    pointPath.lineTo(1,-radius);
    pointPath.lineTo(-1,-radius);
    pointPath.lineTo(-10,0);
    pointPath.arcTo(-10,0,20,20,180,180);
    QPainterPath inRing;
    inRing.addEllipse(-5,-5,10,10);
    painter.save();
    //计算并选择绘图对象坐标
    painter.rotate(second*6);
    //设置画刷
    painter.setBrush(QColor(255,0,0,200));
    //绘制路径
    painter.drawPath(pointPath.subtracted(inRing));
    painter.restore();
}
相关推荐
苏三有春1 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
Vanranrr1 小时前
C++ QT
java·c++·qt
兆。1 小时前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
徒步僧12 小时前
ThingsBoard规则链节点:RPC Call Reply节点详解
qt·microsoft·rpc
可峰科技13 小时前
斗破QT编程入门系列之一:认识Qt:初步使用(四星斗师)
开发语言·qt
我喜欢就喜欢13 小时前
基于qt vs下的视频播放
开发语言·qt·音视频
CP-DD14 小时前
Qt的架构设计
qt
阿_旭15 小时前
基于YOLO11/v10/v8/v5深度学习的维修工具检测识别系统设计与实现【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·qt·ai
Bruce小鬼19 小时前
QT创建按钮篇
开发语言·qt
martian66520 小时前
QT开发:掌握现代UI动画技术:深入解析QML和Qt Quick中的动画效果
开发语言·c++·qt·ui