使用Qt创建模拟时钟

main.cpp

  1. #include <QtGui/QApplication>  
  2. #include "analogclock.h"  
  3.   
  4. Q_DECL_EXPORT int main(int argc, char *argv[])  
  5. {  
  6.     QApplication theApp(argc, argv);  
  7.   
  8.     AnalogClock clock;  
  9.     clock.show();  
  10.   
  11.     return theApp.exec();  
  12. }  

analogclock.h

  1. #ifndef ANALOGCLOCK_H  
  2. #define ANALOGCLOCK_H  
  3.   
  4. #include <QWidget>  
  5.   
  6. class AnalogClock : public QWidget  
  7. {  
  8.     Q_OBJECT  
  9. public:  
  10.     explicit AnalogClock(QWidget *parent = 0);  
  11.   
  12. signals:  
  13.   
  14. public slots:  
  15.   
  16. protected:  
  17.      void paintEvent(QPaintEvent *event);  
  18. };  
  19.   
  20. #endif // ANALOGCLOCK_H void paintEvent(QPaintEvent *event);  


analogclock.cpp


  1. #include <QtGui>  
  2. #include "analogclock.h"  
  3.   
  4. AnalogClock::AnalogClock(QWidget *parent) :  
  5.     QWidget(parent)  
  6. {  
  7.     QTimer* timer = new QTimer(this);  
  8.     connect(timer, SIGNAL(timeout()), this, SLOT(update()));  
  9.     timer->start(1000);  
  10.     setWindowTitle(tr("Analog Clock"));  
  11.     resize(200, 200);  
  12.   
  13. }  
  14.   
  15. void AnalogClock::paintEvent(QPaintEvent *)  
  16. {  
  17.     // 时针和分针的点坐标  
  18.     //画的是三角形,所以需要三个点的坐标  
  19.     static const QPoint hourHand[3] = {  
  20.         QPoint(7, 8),  
  21.         QPoint(-7, 8),  
  22.         QPoint(0, -30)  
  23.     };  
  24.     static const QPoint minuteHand[3] = {  
  25.         QPoint(7, 8),  
  26.         QPoint(-7, 8),  
  27.         QPoint(0, -60)  
  28.     };  
  29.   
  30.     static const QPoint secondHand[3] = {  
  31.         QPoint(4, 8),  
  32.         QPoint(-4, 8),  
  33.         QPoint(0, -100)  
  34.     };  
  35.   
  36.     /* 设置时针和分针的颜色 */  
  37.     QColor hourColor(127, 0, 127);  
  38.     QColor minuteColor(0, 127, 127, 191);  
  39.     QColor secondColor(0, 100, 100);  
  40.   
  41.     /* QT自带函数,返回两个数的最小值 */  
  42.     int side = qMin(width(), height());  
  43.   
  44.     /* 获取当前时间 */  
  45.     QTime time = QTime::currentTime();  
  46.   
  47.     /* 
  48.         在this这个可绘画设备上创建一个绘画者 
  49.         设置渲染,用反锯齿效果 
  50.         用一个重载函数转换一下坐标系 
  51.         缩放一下这个坐标系 
  52.     */  
  53.     //The QPainter class performs low-level painting on widgets and other paint devices.  
  54.     QPainter painter(this);  
  55.   
  56.     //Sets the given render hint on the painter if on is true; otherwise clears the render hint  
  57.     //Renderhints are used to specify flags to QPainter that may or may not be respected by any given engine.  
  58.     painter.setRenderHint(QPainter::Antialiasing);  
  59.   
  60.     //Translates the coordinate system by the given offset  
  61.     painter.translate(width()/2, height()/2);  
  62.   
  63.     //Scales the coordinate system by (sx, sy).  
  64.     painter.scale(side/200.0, side/200.0);  
  65.   
  66.     /* 用一个重载函数设置一下画笔的类型,NoPen就是没有线 
  67.        再用一个重载函数设置一下笔刷的颜色,用默认类型 
  68.     */  
  69.     //Sets the painter's pen to be the given pen.  
  70.     //The pen defines how to draw lines and outlines, and it also defines the text color.  
  71.     painter.setPen(Qt::NoPen);  
  72.   
  73.     //sets the painter's brush to the given brush.  
  74.     //The painter's brush defines how shapes are filled.  
  75.     painter.setBrush(hourColor);  
  76.   
  77.     //Saves the current painter state (pushes the state onto a stack).  
  78.     //A save() must be followed by a corresponding restore(); the end() function unwinds the stack.  
  79.     painter.save(); //保存当前的绘画者状态  
  80.   
  81.     //用给出的角度旋转坐标系  
  82.     //Rotates the coordinate system the given angle clockwise.  
  83.     painter.rotate(30.0*((time.hour()+time.minute()/60.0)));  
  84.   
  85.     //用线把点连起来  
  86.     //Draws the convex polygon defined by the first pointCount points in the array points using the current pen.  
  87.     //The first point is implicitly connected to the last point, and the polygon is filled with the current brush().  
  88.     painter.drawConvexPolygon(hourHand, 3);  
  89.   
  90.     //恢复当前的绘画者状态  
  91.     //Restores the current painter state (pops a saved state off the stack).  
  92.     painter.restore();  
  93.   
  94.     painter.setPen(hourColor);  
  95.   
  96.     /* 画代表小时的线 */  
  97.     for (int i = 0; i < 12; ++i) {  
  98.         //Draws a line from (x1, y1) to (x2, y2) and sets the current pen position to (x2, y2).  
  99.         painter.drawLine(88, 0, 96, 0);  
  100.         painter.rotate(30.0);  
  101.     }  
  102.   
  103.     //下面画分钟的过程是上面的重复,只是分针的颜色用到了透明度  
  104.     painter.setPen(Qt::NoPen);  
  105.     painter.setBrush(minuteColor);  
  106.     painter.save();  
  107.     painter.rotate(6.0*(time.minute()+time.second()/60.0));  
  108.     painter.drawConvexPolygon(minuteHand, 3);  
  109.     painter.restore();  
  110.     painter.setPen(minuteColor);  
  111.   
  112.     /* 画代表分钟的线 */  
  113.     for (int j = 0; j < 60; ++j) {  
  114.         if((j % 5) != 0) {  
  115.             painter.drawLine(92, 0, 96,0);  
  116.         }  
  117.         painter.rotate(6.0);  
  118.     }  
  119.   
  120.     //下面画秒针的过程是上面的重复  
  121.     painter.setPen(Qt::NoPen);  
  122.     painter.setBrush(secondColor);  
  123.     painter.save();  
  124.     painter.rotate(6.0*(time.second()));  
  125.     painter.drawConvexPolygon(secondHand, 3);  
  126.     painter.restore();  
  127.     painter.setPen(minuteColor);  
  128. }  



FROM: http://www.cnblogs.com/johnpher/archive/2012/01/01/2570587.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值