main.cpp
- #include <QtGui/QApplication>
- #include "analogclock.h"
- Q_DECL_EXPORT int main(int argc, char *argv[])
- {
- QApplication theApp(argc, argv);
- AnalogClock clock;
- clock.show();
- return theApp.exec();
- }
analogclock.h
- #ifndef ANALOGCLOCK_H
- #define ANALOGCLOCK_H
- #include <QWidget>
- class AnalogClock : public QWidget
- {
- Q_OBJECT
- public:
- explicit AnalogClock(QWidget *parent = 0);
- signals:
- public slots:
- protected:
- void paintEvent(QPaintEvent *event);
- };
- #endif // ANALOGCLOCK_H void paintEvent(QPaintEvent *event);
analogclock.cpp
- #include <QtGui>
- #include "analogclock.h"
- AnalogClock::AnalogClock(QWidget *parent) :
- QWidget(parent)
- {
- QTimer* timer = new QTimer(this);
- connect(timer, SIGNAL(timeout()), this, SLOT(update()));
- timer->start(1000);
- setWindowTitle(tr("Analog Clock"));
- resize(200, 200);
- }
- void AnalogClock::paintEvent(QPaintEvent *)
- {
- // 时针和分针的点坐标
- //画的是三角形,所以需要三个点的坐标
- static const QPoint hourHand[3] = {
- QPoint(7, 8),
- QPoint(-7, 8),
- QPoint(0, -30)
- };
- static const QPoint minuteHand[3] = {
- QPoint(7, 8),
- QPoint(-7, 8),
- QPoint(0, -60)
- };
- static const QPoint secondHand[3] = {
- QPoint(4, 8),
- QPoint(-4, 8),
- QPoint(0, -100)
- };
- /* 设置时针和分针的颜色 */
- QColor hourColor(127, 0, 127);
- QColor minuteColor(0, 127, 127, 191);
- QColor secondColor(0, 100, 100);
- /* QT自带函数,返回两个数的最小值 */
- int side = qMin(width(), height());
- /* 获取当前时间 */
- QTime time = QTime::currentTime();
- /*
- 在this这个可绘画设备上创建一个绘画者
- 设置渲染,用反锯齿效果
- 用一个重载函数转换一下坐标系
- 缩放一下这个坐标系
- */
- //The QPainter class performs low-level painting on widgets and other paint devices.
- QPainter painter(this);
- //Sets the given render hint on the painter if on is true; otherwise clears the render hint
- //Renderhints are used to specify flags to QPainter that may or may not be respected by any given engine.
- painter.setRenderHint(QPainter::Antialiasing);
- //Translates the coordinate system by the given offset
- painter.translate(width()/2, height()/2);
- //Scales the coordinate system by (sx, sy).
- painter.scale(side/200.0, side/200.0);
- /* 用一个重载函数设置一下画笔的类型,NoPen就是没有线
- 再用一个重载函数设置一下笔刷的颜色,用默认类型
- */
- //Sets the painter's pen to be the given pen.
- //The pen defines how to draw lines and outlines, and it also defines the text color.
- painter.setPen(Qt::NoPen);
- //sets the painter's brush to the given brush.
- //The painter's brush defines how shapes are filled.
- painter.setBrush(hourColor);
- //Saves the current painter state (pushes the state onto a stack).
- //A save() must be followed by a corresponding restore(); the end() function unwinds the stack.
- painter.save(); //保存当前的绘画者状态
- //用给出的角度旋转坐标系
- //Rotates the coordinate system the given angle clockwise.
- painter.rotate(30.0*((time.hour()+time.minute()/60.0)));
- //用线把点连起来
- //Draws the convex polygon defined by the first pointCount points in the array points using the current pen.
- //The first point is implicitly connected to the last point, and the polygon is filled with the current brush().
- painter.drawConvexPolygon(hourHand, 3);
- //恢复当前的绘画者状态
- //Restores the current painter state (pops a saved state off the stack).
- painter.restore();
- painter.setPen(hourColor);
- /* 画代表小时的线 */
- for (int i = 0; i < 12; ++i) {
- //Draws a line from (x1, y1) to (x2, y2) and sets the current pen position to (x2, y2).
- painter.drawLine(88, 0, 96, 0);
- painter.rotate(30.0);
- }
- //下面画分钟的过程是上面的重复,只是分针的颜色用到了透明度
- painter.setPen(Qt::NoPen);
- painter.setBrush(minuteColor);
- painter.save();
- painter.rotate(6.0*(time.minute()+time.second()/60.0));
- painter.drawConvexPolygon(minuteHand, 3);
- painter.restore();
- painter.setPen(minuteColor);
- /* 画代表分钟的线 */
- for (int j = 0; j < 60; ++j) {
- if((j % 5) != 0) {
- painter.drawLine(92, 0, 96,0);
- }
- painter.rotate(6.0);
- }
- //下面画秒针的过程是上面的重复
- painter.setPen(Qt::NoPen);
- painter.setBrush(secondColor);
- painter.save();
- painter.rotate(6.0*(time.second()));
- painter.drawConvexPolygon(secondHand, 3);
- painter.restore();
- painter.setPen(minuteColor);
- }
FROM: http://www.cnblogs.com/johnpher/archive/2012/01/01/2570587.html