Results 1 to 3 of 3

Thread: QPainter and Qt analog clock example

  1. #1
    Join Date
    Jun 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPainter and Qt analog clock example

    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 

  2. #2
    Join Date
    Aug 2009
    Posts
    52
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter and Qt analog clock example

    from QPainter Class Reference
    Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

  3. #3
    Join Date
    Jun 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter and Qt analog clock example

    So I just have to call paintEvent and update the variables like angle elsewhere?

Similar Threads

  1. how to get QPainter object for QLabel
    By bpatel in forum Newbie
    Replies: 7
    Last Post: 28th May 2010, 10:30
  2. Customizable Analog clock general advice needed
    By been_1990 in forum Qt Programming
    Replies: 0
    Last Post: 21st September 2009, 18:17
  3. QPainter drawText with different language
    By lni in forum Qt Programming
    Replies: 4
    Last Post: 11th June 2009, 19:54
  4. QPainter and QImage
    By beerkg in forum Newbie
    Replies: 3
    Last Post: 7th September 2006, 14:48
  5. Replies: 7
    Last Post: 20th March 2006, 20:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.