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();
}
相关推荐
徐霞客3203 小时前
Qt入门1——认识Qt的几个常用头文件和常用函数
开发语言·c++·笔记·qt
姆路4 小时前
QT Designer内存飙升
qt
Bruce小鬼6 小时前
QT文件基本操作
开发语言·qt
懷淰メ6 小时前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
Mr.Q11 小时前
OpenCV和Qt坐标系不一致问题
qt·opencv
重生之我是数学王子14 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
----云烟----1 天前
QT中QString类的各种使用
开发语言·qt
「QT(C++)开发工程师」1 天前
【qt版本概述】
开发语言·qt
一路冰雨1 天前
Qt打开文件对话框选择文件之后弹出两次
开发语言·qt
老赵的博客1 天前
QT 自定义界面布局要诀
开发语言·qt