QT画钟表

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui>

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    Widget(QWidget *parent = 0);
    void paintEvent(QPaintEvent * event);
    ~Widget();
};

#endif // WIDGET_H

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    resize(200,200);
}

Widget::~Widget()
{
    
}

void Widget::paintEvent(QPaintEvent * event){

    QPainter painter(this);
    static QPoint hourHand[3] = {QPoint(-7,7),QPoint(7,7),QPoint(0,-40)};
    QColor hourColor(127,0,127);
    QColor minColor(0, 127, 127, 191);
    static QPoint minHand[3] = {QPoint(-7,7),QPoint(7,7),QPoint(0,-70)};
    painter.setRenderHint(QPainter::Antialiasing);//抗锯齿
    painter.translate(this->width()/2,this->height()/2);
    QTime time = QTime::currentTime();
    painter.save();
    painter.rotate(30*time.hour() + time.minute()/60);
    painter.setBrush(hourColor);
    painter.drawConvexPolygon(hourHand,3);
    painter.restore();

    painter.save();
    painter.rotate(6*time.minute()+time.second()/60);
    painter.setBrush(minColor);
    painter.drawConvexPolygon(minHand,3);
    painter.restore();

    painter.setPen(hourColor);
    for(int ii = 0;ii<12;ii++)
    {
        painter.drawLine(QPoint(88,0),QPoint(96,0));
        painter.rotate(30);
    }
    painter.setPen(minColor);
    for(int jj = 0;jj<60;jj++){

        if(jj%5==0)
        {
            painter.rotate(6);
            continue;
        }
        painter.drawLine(QPoint(90,0),QPoint(94,0));
        painter.rotate(6);
    }
}

#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    
    return a.exec();
}

你可能感兴趣的:(QT画钟表)