⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clockface.cpp

📁 程序代码使用说明: (1)所有源代码目录下都提供了Makefile(非Qt)
💻 CPP
字号:
#include "ClockFace.h"#include <QLayout>#include <QPainter>#include <QImage>#include <QPaintEvent>#include <math.h>const double deg2rad = 0.017453292519943295769; // pi/180ClockFace::ClockFace(QWidget *parent)    : QFrame(parent), isEvent(true){    setMinimumSize(50,50);    setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));        currTime = QTime::currentTime();}void ClockFace::paintEvent( QPaintEvent *event ){    QPainter paint(this);    if (event->rect().intersects(contentsRect())) {        paint.setClipRegion(event->region().intersect(contentsRect()));        drawContents(&paint);    }}void ClockFace::drawContents( QPainter *pp ){    pp->setRenderHint(QPainter::Antialiasing);    QRect r = contentsRect();    int size = qMin(r.width(), r.height());    QPoint offs((r.width()-size)/2, (r.height()-size)/2);    QRect pr(0, 0, size, size);    pp->save();    pp->translate(offs.x(), offs.y());    pp->setRenderHint(QPainter::Antialiasing);    isEvent = true;    QPoint center( size / 2, size / 2 );    const int w_tick = pr.width()/150+1;    const int w_sec = pr.width()/200+1;    const int w_min = pr.width()/100+1;    const int w_hour = pr.width()/50+1;    QPoint l1( pr.x() + pr.width() / 2, pr.y() + 2 );    QPoint l2( pr.x() + pr.width() / 2, pr.y() + 6 );    QPoint h1( pr.x() + pr.width() / 2, pr.y() + pr.height() / 4 );    QPoint h2( pr.x() + pr.width() / 2, pr.y() + pr.height() / 2 );    QPoint m1( pr.x() + pr.width() / 2, pr.y() + pr.height() / 9 );    QPoint m2( pr.x() + pr.width() / 2, pr.y() + pr.height() / 2 );    QPoint s1( pr.x() + pr.width() / 2, pr.y() + 8 );    QPoint s2( pr.x() + pr.width() / 2, pr.y() + pr.height() / 2 );    QColor color(Qt::black);    QTime time = currTime;    if ( isEvent || prevTime.minute() != currTime.minute()            || prevTime.hour() != currTime.hour()            || qAbs(prevTime.secsTo(currTime)) > 1 )     {        // draw ticks        pp->setPen(QPen(color, w_tick));        for ( int i = 0; i < 12; i++ )            pp->drawLine( rotate( center, l1, i * 30 ), rotate( center, l2, i * 30 ) );        // draw hour pointer        h1 = rotate( center, h1, 30 * ( time.hour() % 12 ) + time.minute() / 2 );        h2 = rotate( center, h2, 30 * ( time.hour() % 12 ) + time.minute() / 2 );        pp->setPen(QPen(Qt::green, w_hour));        pp->drawLine(h1, h2);        // draw minute pointer        m1 = rotate( center, m1, time.minute() * 6 );        m2 = rotate( center, m2, time.minute() * 6 );        pp->setPen(QPen(Qt::blue, w_min));        pp->drawLine(m1, m2);    }    pp->restore();    prevTime = currTime;    pp->setClipping(false);    // draw second pointer    s1 = rotate( center, s1, time.second() * 6 );    s2 = rotate( center, s2, time.second() * 6 );    pp->setPen(QPen(Qt::red, w_sec));    pp->drawLine(s1+offs, s2+offs);    // cap    pp->setBrush(color);    pp->drawEllipse( center.x()-w_hour/2+offs.x(), center.y()-w_hour/2+offs.y(), w_hour, w_hour );    isEvent = true;}void ClockFace::display( const QTime& t ){    currTime = t;    if (isVisible())         isEvent = false;    repaint();}QPoint ClockFace::rotate( QPoint c, QPoint p, int a ){    double angle = deg2rad * ( - a + 180 );    double nx = c.x() - ( p.x() - c.x() ) * cos( angle ) -                ( p.y() - c.y() ) * sin( angle );    double ny = c.y() - ( p.y() - c.y() ) * cos( angle ) +                ( p.x() - c.x() ) * sin( angle );    return QPoint( int(nx), int(ny) );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -