Results 1 to 14 of 14

Thread: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

  1. #1
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Question QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Good Day,

    //I'm fairly proficient in qt -> got some problems with interactive programming as:

    1) I created a simple label
    2) Uploaded Image to Label
    3) using mouseEvents & paintEvent was able to create point on Image
    4) MY PROBLEM: the 2nd mouse click! //& store 2nd point where?

    User needs to click twice to get 2 points(line will be drawn between them, however everytime in the running program! when I click for a second time, the initial point becomes the new point.

    some att code:
    void label::mousePressEvent(QMouseEvent *event)
    {
    paintFlag = 0;
    if(event->button()==Qt::LeftButton)
    {
    this->x = ev->x();
    this->y = ev->y();
    //store 1st point
    x1 = x;
    y1 = y;
    emit Mouse_Pressed();
    paintFlag = 1;

    //Mouse Pressed Again! 2nd point
    x2 = x;
    y2 = y;
    emit Mouse_Pressed();
    }

    }

    paintEvent:
    QPainter painter(this);
    QPen paintpen(Qt::red);
    paintpen.setWidth(4);

    QPoint p1;
    p1.setX(x1);
    p1.setY(y1);

    painter.setPen(paintpen);
    painter.drawPoint(p1);

    }


    Any ideas on my problem... what am i not seeing!
    Thanks
    Last edited by ebsaith; 10th June 2013 at 13:08.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Qt Code:
    1. void label::mousePressEvent(QMouseEvent *event)
    2. {
    3. paintFlag = 0;
    4. if(event->button()==Qt::LeftButton)
    5. {
    6. this->x = ev->x();
    7. this->y = ev->y();
    8.  
    9. //store 1st point
    10. if(firstClick) // set firstClick = true; in ctor
    11. {
    12. x1 = x;
    13. y1 = y;
    14. emit Mouse_Pressed();
    15. firstClick = false;
    16. }
    17. paintFlag = 1;
    18.  
    19. //Mouse Pressed Again! 2nd point
    20. if(!firstClick) //EDIT: corrected
    21. {
    22. x2 = x;
    23. y2 = y;
    24. emit Mouse_Pressed();
    25. firstClick = true;
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 10th June 2013 at 15:08.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Thanks for the quick response, however as I implemented that
    as the program begins it automatically creates a point at (0, 0) and a point following my mouse pointer -> moving line as mouse hovers around... its cool but not what i want it to do... new problem derived from prev1.

    attached my drawFunc:
    Qt Code:
    1. void my_qlabel::paintEvent(QPaintEvent *e)
    2. {
    3. if(paintFlag == 1)
    4. {
    5. QPainter painter(this);
    6. QPen paintpen(Qt::red);
    7. paintpen.setWidth(4);
    8.  
    9. QPen linepen(Qt::black);
    10. linepen.setWidth(4);
    11.  
    12. QPoint p1;
    13. p1.setX(x);
    14. p1.setY(y);
    15.  
    16.  
    17. QPoint p2;
    18. p2.setX(x2);
    19. p2.setY(y2);
    20.  
    21. painter.setPen(linepen);
    22. painter.drawLine(p1, p2);
    23.  
    24. painter.setPen(paintpen);
    25. painter.drawPoint(p1);
    26.  
    27. painter.setPen(paintpen);
    28. painter.drawPoint(p2);
    29.  
    30. //paintFlag = 2;
    31.  
    32. }
    33. update();
    34. }
    To copy to clipboard, switch view to plain text mode 

    Also note: the paintEvent starts immediately hence i put a flagCounter to initiate it ?

    Kind Regards

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Post the complete code again

  5. #5
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Qt Code:
    1. void my_qlabel::mousePressEvent(QMouseEvent *ev)
    2. {
    3. paintFlag = 0;
    4. if(ev->button()==Qt::LeftButton)
    5. {
    6. this->x = ev->x();
    7. this->y = ev->y();
    8.  
    9. //store 1st point
    10. if(firstClick)
    11. {
    12. this->x = ev->x();
    13. this->y = ev->y();
    14. x1 = x;
    15. y1 = y;
    16. emit Mouse_Pressed();
    17. firstClick = false;
    18. }
    19.  
    20. //Mouse Pressed Again! 2nd point
    21. if(firstClick)
    22. {
    23. x2 = x;
    24. y2 = y;
    25. emit Mouse_Pressed();
    26. firstClick = true;
    27. }
    28.  
    29. paintFlag = 1;
    30.  
    31. //calculateAngle(x, y, x1, y2);
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    paint event:
    Qt Code:
    1. void my_qlabel::paintEvent(QPaintEvent *e)
    2. {
    3. if(paintFlag == 1)
    4. {
    5. QPainter painter(this);
    6. QPen paintpen(Qt::red);
    7. paintpen.setWidth(4);
    8.  
    9. QPen linepen(Qt::black);
    10. linepen.setWidth(4);
    11.  
    12. QPoint p1;
    13. p1.setX(x1);
    14. p1.setY(y1);
    15.  
    16. painter.setPen(paintpen);
    17. painter.drawPoint(p1);
    18.  
    19. QPoint p2;
    20. p2.setX(x2);
    21. p2.setY(y2);
    22.  
    23. painter.setPen(paintpen);
    24. painter.drawPoint(p2);
    25.  
    26. painter.setPen(linepen);
    27. painter.drawLine(p1, p2);
    28.  
    29. }
    30. update();
    31. }
    To copy to clipboard, switch view to plain text mode 

    It starts from point(0, 0) instead of where user clicks
    when user does click it creates an end point & a line from the (0, 0)

    Goal: user must click start point, click end point, line to be drawn between 2 points, and points must be saved for later calculations

    I know moreOrLess how to do it, just cant seem to get my head around this mouse clicking

    Kind Regards

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Try this correction
    Qt Code:
    1. void my_qlabel::mousePressEvent(QMouseEvent *ev)
    2. {
    3. paintFlag = 0;
    4. if(ev->button()==Qt::LeftButton)
    5. {
    6. this->x = ev->x();
    7. this->y = ev->y();
    8.  
    9. //store 1st point
    10. if(firstClick)
    11. {
    12. this->x = ev->x();
    13. this->y = ev->y();
    14. x1 = x;
    15. y1 = y;
    16. emit Mouse_Pressed();
    17. firstClick = false;
    18. }
    19.  
    20. //Mouse Pressed Again! 2nd point
    21. if(!firstClick)
    22. {
    23. x2 = x;
    24. y2 = y;
    25. emit Mouse_Pressed();
    26. firstClick = true;
    27. paintFlag = 1;
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Tried it -> unfortunately Back to square 1
    creates a single point wherever the user clicks... User needs to click twice to get 2 points(line will be drawn between them, however every time in the running program! when I click for a second time, the initial point becomes the new point.) no line shown

    this twist in the tail got me stranded

    any other ideas?

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Qt Code:
    1. #include <QtGui>
    2. #include <QtWidgets>
    3. #include <QApplication>
    4.  
    5. class Label : public QLabel
    6. {
    7. public:
    8. explicit Label(QWidget * parent = 0)
    9. : QLabel(parent)
    10. , mStartX(0)
    11. , mStartY(0)
    12. , mEndX(0)
    13. , mEndY(0)
    14. , mFirstClick(true)
    15. , mPaintFlag(false)
    16. {
    17. ;
    18. }
    19.  
    20. protected:
    21. void mousePressEvent(QMouseEvent * e)
    22. {
    23. if(e->button() == Qt::LeftButton)
    24. {
    25. //store 1st point
    26. if(mFirstClick)
    27. {
    28. mStartX = e->x();
    29. mStartY = e->y();
    30. mFirstClick = false;
    31. }
    32. //Mouse Pressed Again! 2nd point
    33. else if(!mFirstClick)
    34. {
    35. mEndX = e->x();
    36. mEndY = e->y();
    37. mFirstClick = true;
    38. mPaintFlag = true;
    39. update();
    40. }
    41. }
    42. }
    43.  
    44. void paintEvent(QPaintEvent * e)
    45. {
    46. QLabel::paintEvent(e);
    47.  
    48. if(mPaintFlag)
    49. {
    50. QPainter painter(this);
    51. QPen paintpen(Qt::red);
    52. paintpen.setWidth(4);
    53.  
    54. QPen linepen(Qt::black);
    55. linepen.setWidth(4);
    56.  
    57. QPoint p1;
    58. p1.setX(mStartX);
    59. p1.setY(mStartY);
    60.  
    61. painter.setPen(paintpen);
    62. painter.drawPoint(p1);
    63.  
    64. QPoint p2;
    65. p2.setX(mEndX);
    66. p2.setY(mEndY);
    67.  
    68. painter.setPen(paintpen);
    69. painter.drawPoint(p2);
    70.  
    71. painter.setPen(linepen);
    72. painter.drawLine(p1, p2);
    73. }
    74. }
    75.  
    76. private:
    77. int mStartX;
    78. int mStartY;
    79. int mEndX;
    80. int mEndY;
    81.  
    82. bool mFirstClick;
    83. bool mPaintFlag;
    84. };
    85.  
    86. int main(int argc, char *argv[])
    87. {
    88. QApplication app(argc, argv);
    89.  
    90. Label w;
    91. w.show();
    92.  
    93. return app.exec();
    94. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. The following user says thank you to Santosh Reddy for this useful post:

    ebsaith (10th June 2013)

  10. #9
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Thanks Santosh

    Highly appreciated

    I Might have future queries on future logical & syntax bugs
    Will add you in future post.

    Kind Regards

  11. #10
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Question Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Good Day,

    Once again I find myself in a stumbling block!

    Once above program created - It saves 2 points and draws line between them.
    New Issue:
    1) if my Paint Event Name is as above "void mmetLabel::paintEvent(QPaintEvent *ev)"
    i'm able to draw line between 2 points and displayed. however i'm unable to load image.

    2) however if I change Paint Event Name as "void mmetLabel::drawSometing(QPaintEvent *ev)"
    then my picture upload function works (Pic Displayed), 2 points are saved... BUT points and lines not displayed

    Any Ideas?

  12. #11
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Call the base paintEvent.
    Qt Code:
    1. void mmetLabel::paintEvent(QPaintEvent *ev)
    2. {
    3. QLabel::paintEvent(ev); //<<<<<<<<< Add this as first line
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  13. The following user says thank you to Santosh Reddy for this useful post:

    ebsaith (11th June 2013)

  14. #12
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Works like a dream!
    Thanks, Santosh.

    Any good Books you know that will make me more proficient in qt

    Thanks again

  15. #13
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    IMO Books provide you with content and may be examples, one becomes provicient by practice.

    Anyway refer this Post
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  16. The following user says thank you to Santosh Reddy for this useful post:

    ebsaith (11th June 2013)

  17. #14
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them

    Kind Regards
    Last edited by ebsaith; 11th June 2013 at 14:29.

Similar Threads

  1. Replies: 3
    Last Post: 22nd February 2013, 20:56
  2. Replies: 3
    Last Post: 12th May 2010, 14:11
  3. Generating Mouse Clicks
    By Ebonair in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2009, 19:20
  4. traversing tree using mouse clicks
    By krishna.bv in forum Qt Programming
    Replies: 3
    Last Post: 22nd December 2006, 10:15
  5. Detecting mouse clicks outside of widget
    By init2null in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2006, 20:16

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.