Hi.

I used this example as a template: http://doc.trolltech.com/4.5/widgets-analogclock.html

What is the correct way to repaint clock if I want to repaint hand when button is pressed? If I use paintEvent() function to initialize clock and other function to repaint hand it doesn't work because painter is not active when button is pressed.

Should I even reimplement paintEvent() function?

I tried something like this but how do I paint this to the widget?

Qt Code:
  1. void Clock::init()
  2. {
  3. static const QPoint hand[3] = {
  4. QPoint(7, 8),
  5. QPoint(-7, 8),
  6. QPoint(0, -90)
  7. };
  8.  
  9. QColor handColor(204, 0, 0);
  10. QColor lineColor(0, 0, 0);
  11.  
  12. int side = qMin(width(), height());
  13.  
  14. QPainter painter(this);
  15. painter.setRenderHint(QPainter::Antialiasing);
  16. painter.translate(width() / 2, height() / 2);
  17. painter.scale(side / 200.0, side / 200.0);
  18. painter.setPen(Qt::NoPen);
  19. painter.setBrush(handColor);
  20. painter.save();
  21.  
  22.  
  23. painter.rotate(0.0);
  24. painter.drawConvexPolygon(hand, 3);
  25. painter.restore();
  26.  
  27. painter.setPen(lineColor);
  28. for (int i=0; i < 12; ++i) {
  29. painter.drawLine(88, 0, 96, 0);
  30. painter.rotate(30.0);
  31. }
  32. }
  33.  
  34. void Clock::btnPressed(qreal angle)
  35. {
  36. static const QPoint hand[3] = {
  37. QPoint(7, 8),
  38. QPoint(-7, 8),
  39. QPoint(0, -40)
  40. };
  41.  
  42. QColor handColor(204, 0 ,0);
  43.  
  44. int side = qMin(width(), height());
  45.  
  46. QPainter painter(this);
  47. painter.setRenderHint(QPainter::Antialiasing);
  48. painter.translate(width() / 2, height() / 2);
  49. painter.scale(side / 200.0, side / 200.0);
  50. painter.setPen(Qt::NoPen);
  51. painter.setBrush(handColor);
  52. painter.save();
  53.  
  54. painter.rotate(angle);
  55. painter.drawConvexPolygon(hand, 3);
  56. painter.restore();
  57. }
To copy to clipboard, switch view to plain text mode