qt 实现钟表图标

  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. }
上一篇:hdu 4608 I-number 大整数


下一篇:.net 文件下载