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();
}
相关推荐
矛取矛求6 小时前
QT的前景与互联网岗位发展
开发语言·qt
Leventure_轩先生6 小时前
[WASAPI]从Qt MultipleMedia来看WASAPI
开发语言·qt
晓纪同学15 小时前
QT-简单视觉框架代码
开发语言·qt
威桑15 小时前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
飞飞-躺着更舒服16 小时前
【QT】实现电子飞行显示器(简易版)
开发语言·qt
fyzy16 小时前
Qt获取本地计算的CPU温度
qt
cbdg375716 小时前
Qt 6 QML Settings location 不创建指定路径文件
qt
了一li16 小时前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
杨德杰16 小时前
QT网络(一):主机信息查询
网络·qt
黄金右肾16 小时前
Qt之串口设计-线程实现(十二)
qt·thread·serialport