Results 1 to 3 of 3

Thread: QPainter: line drawing along a circle

  1. #1
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPainter: line drawing along a circle

    Hi All!
    I have a class:
    widget.h
    Qt Code:
    1. class Widget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. QPoint mouseOldPos;
    6. QPoint mousePos;
    7. QImage *image;
    8.  
    9. public:
    10. Widget( QWidget *parent = 0 );
    11. ~Widget();
    12.  
    13. protected:
    14. void paintEvent( QPaintEvent *e );
    15. void draw( QPainter *painter );
    16. void mousePressEvent( QMouseEvent *e );
    17. void mouseReleaseEvent( QMouseEvent *e );
    18. void mouseMoveEvent( QMouseEvent *e );
    19.  
    20. protected slots:
    21. void createImage();
    22. };
    To copy to clipboard, switch view to plain text mode 
    widget.cpp
    Qt Code:
    1. Widget::Widget( QWidget *parent ):
    2. QWidget( parent ),
    3. image(0)
    4. {
    5. setAttribute( Qt::WA_StaticContents );
    6. setCursor( Qt::CrossCursor );
    7. setMouseTracking( true );
    8. setFocusPolicy( Qt::WheelFocus );
    9. setFocus();
    10.  
    11. setFixedSize( 800, 600 );
    12.  
    13. QTimer::singleShot( 500, this, SLOT(createImage()) );
    14. }
    15. //=================================================================================================
    16. Widget::~Widget()
    17. {
    18. if (image)
    19. {
    20. delete image;
    21. image = 0;
    22. }
    23. }
    24. //=================================================================================================
    25. void Widget::paintEvent( QPaintEvent *e )
    26. {
    27. if (image)
    28. {
    29. QPainter imagePainter( image );
    30. imagePainter.setRenderHint( QPainter::Antialiasing, true );
    31. imagePainter.eraseRect( rect() );
    32. draw( &imagePainter );
    33. imagePainter.end();
    34.  
    35. QPainter painter( this );
    36. QRect dirtyRect = e->rect();
    37. painter.drawImage( dirtyRect, (*image), dirtyRect );
    38. }
    39. }
    40. //=================================================================================================
    41. void Widget::draw( QPainter *painter )
    42. {
    43. painter->save();
    44. painter->translate( width() / 2, height() / 2 );
    45. painter->setPen( QColor( 0, 0, 0, 255 ) );
    46. painter->drawEllipse( QPoint( 0, 90 ), 90, 90 );
    47. painter->restore();
    48.  
    49. painter->save();
    50. painter->setPen( QPen( Qt::green, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
    51. painter->drawLine( QPoint( width() / 2, height() / 2 + 90 ), mousePos );
    52. painter->restore();
    53. }
    54. //=================================================================================================
    55. void Widget::mousePressEvent( QMouseEvent *e )
    56. {
    57. if (e->buttons() & Qt::LeftButton)
    58. {
    59. mouseOldPos = e->pos();
    60. update();
    61. }
    62. }
    63. //=================================================================================================
    64. void Widget::mouseReleaseEvent( QMouseEvent *e )
    65. {
    66. update();
    67. e->accept();
    68. }
    69. //=================================================================================================
    70. void Widget::mouseMoveEvent( QMouseEvent *e )
    71. {
    72. mousePos = e->pos();
    73. update();
    74. }
    75. //=================================================================================================
    76. void Widget::createImage()
    77. {
    78. if (!image)
    79. {
    80. image = new QImage( size(), QImage::Format_ARGB32_Premultiplied );
    81. image->fill( Qt::white );
    82.  
    83. update();
    84. }
    85. }
    To copy to clipboard, switch view to plain text mode 
    How can I draw part of line, selected in red?
    Attachment 7504
    http://saveimg.ru/show-image.php?id=...2f4d7b2c54d30a
    Last edited by Jonny174; 15th March 2012 at 03:43.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPainter: line drawing along a circle

    Try again. The attachment is broken.

    Oh, and have a look at QPainter::drawArc() just in case that's what you were looking for.


    Edit: Now you have posted a picture I have no idea what you want. What does "along a circle" have to do with the straight line segment in red? Seems like a simple case of calculate the end points, set a pen, draw a line.
    Last edited by ChrisW67; 15th March 2012 at 03:48.

  3. #3
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [SOLVED] QPainter: line drawing along a circle

    SOLVE:
    Qt Code:
    1. float mouse_radius = getDistanceBetweenPoints( width() / 2, height() / 2 + 90, mousePos.x(), mousePos.y() );
    2.  
    3. float pX = width() / 2 + 90 * (mousePos.x() - width() / 2) / mouse_radius;
    4. float pY = (height() / 2 + 90) + 90 * (mousePos.y() - (height() / 2 + 90)) / mouse_radius;
    5.  
    6. float pX2 = width() / 2 + 80 * (mousePos.x() - width() / 2) / mouse_radius;
    7. float pY2 = (height() / 2 + 90) + 80 * (mousePos.y() - (height() / 2 + 90)) / mouse_radius;
    8.  
    9. painter->drawLine( int( pX2 ), int( pY2 ), int( pX ), int( pY ) );
    10.  
    11.  
    12.  
    13. float getVectorLength( float dx, float dy )
    14. {
    15. return qSqrt( dx * dx + dy * dy );
    16. }
    17.  
    18. float getDistanceBetweenPoints( float x1, float y1, float x2, float y2 )
    19. {
    20. return getVectorLength( x2 - x1, y2 - y1 );
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Jonny174; 15th March 2012 at 06:06. Reason: SOLVED

Similar Threads

  1. Combining QPainter and WinAPI drawing
    By kkk777kkk in forum Newbie
    Replies: 2
    Last Post: 6th July 2011, 14:16
  2. QPainter immediate drawing outside paint event
    By sfcheng77 in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2011, 06:39
  3. Text drawing with QPainter
    By Rambobino in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2010, 17:26
  4. QPainter and complex drawing : advices
    By toutarrive in forum Qt Programming
    Replies: 0
    Last Post: 18th November 2009, 10:51
  5. Line drawing
    By kiranraj in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2007, 06:38

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
  •  
Qt is a trademark of The Qt Company.