Chinaunix首页 | 论坛 | 博客
  • 博客访问: 137502
  • 博文数量: 32
  • 博客积分: 582
  • 博客等级: 中士
  • 技术积分: 291
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-24 22:38
文章分类

全部博文(32)

文章存档

2012年(3)

2011年(29)

我的朋友

分类: 嵌入式

2011-12-23 21:04:06

这是本人参照网上的资料,然后稍作修改,完善了的时钟界面,界面如下。
功能比较简单,主要是读取当前主机的时间,然后显示,可随意放大缩小。
附源码:
 
  1. #include "clock.h"

  2. CLOCK::CLOCK(QWidget *parent)
  3.     : QWidget(parent)
  4. {
  5.     QTimer *timer = new QTimer(this);
  6.     timer->start(1000);//一秒钟
  7.     connect(timer,SIGNAL(timeout()),this,SLOT(update()));

  8.     QFont font;
  9.     font.setPointSize(10);//字体大小设置为10
  10.     setFont(font);
  11. }

  12. CLOCK::~CLOCK()
  13. {

  14. }

  15. void CLOCK::paintEvent(QPaintEvent * /* event */)
  16. {
  17.     QPainter painter(this);
  18.     painter.setRenderHint(QPainter::Antialiasing, true);

  19.     int side = qMin(width(), height());

  20.     painter.setViewport((width() - side) / 2, (height() - side) / 2,
  21.                         side, side);
  22.     painter.setWindow(0, 0, 200, 200);/*设置(0,0)为新的坐标原点*/

  23.     draw(&painter);
  24. }

  25. void CLOCK::draw(QPainter *painter)
  26. {
  27.     QPen thickPen(palette().foreground(), 1.5);//设置刻度线为粗黑
  28.     QPen thinPen(palette().foreground(), 0.5);//设置刻度线为灰

  29.     QTime time = QTime::currentTime();
  30.     painter->translate(100,100);//重新设定坐标原点

  31.     painter->setRenderHint(QPainter::Antialiasing);//消锯齿
  32.     painter->setBrush(Qt::red);
  33.     painter->setPen(Qt::red);
  34.     painter->save();//保存坐标系,防止坐标系跑偏了
  35.     painter->rotate(6.0*time.second());//注意是6.0,不是6
  36.     painter->drawConvexPolygon(sed,4);
  37.     painter->restore();//复位之前的坐标系

  38.     painter->setBrush(Qt::blue);
  39.     painter->setPen(Qt::blue);
  40.     painter->save();
  41.     painter->rotate(6.0*(time.minute()+time.second()/60.0));
  42.     painter->drawConvexPolygon(min,4);
  43.     painter->restore();

  44.     painter->setBrush(Qt::black);
  45.     painter->setPen(Qt::black);
  46.     painter->save();
  47.     painter->rotate(30.0*(time.hour()+time.minute()/60.0));
  48.     painter->drawConvexPolygon(hour,4);
  49.     painter->restore();

  50. // //painter->drawLine(0,-98,0,-88);
  51. // for(int i=0;i<12;i++)
  52. // {
  53. // painter->rotate(30);//坐标轴旋转30度
  54. // painter->drawLine(0,-98,0,-88);
  55. // }
  56.     /*画刻度还有数字*/
  57.     for (int i = 1; i <=60; ++i) {
  58.         painter->save();
  59.         painter->rotate(6*i);//坐标轴旋转6度
  60.         if (i % 5 == 0) {
  61.             painter->setPen(thickPen);
  62.             painter->drawLine(0, -98, 0, -82);
  63.             painter->drawText(-20, -82, 40, 40,
  64.                               Qt::AlignHCenter | Qt::AlignTop,
  65.                               QString::number(i/5));
  66.         } else {
  67.             painter->setPen(thinPen);
  68.             painter->drawLine(0, -98, 0, -88);
  69.         }
  70.         //painter->rotate(-DegreesPerMinute);
  71.         painter->restore();
  72.     }
  73. }
阅读(12958) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~