PDA

View Full Version : QPaint - Saving coordinates of 2 mouse clicks and the drawing line between them



ebsaith
10th June 2013, 11:05
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

Santosh Reddy
10th June 2013, 12:31
void label::mousePressEvent(QMouseEvent *event)
{
paintFlag = 0;
if(event->button()==Qt::LeftButton)
{
this->x = ev->x();
this->y = ev->y();

//store 1st point
if(firstClick) // set firstClick = true; in ctor
{
x1 = x;
y1 = y;
emit Mouse_Pressed();
firstClick = false;
}
paintFlag = 1;

//Mouse Pressed Again! 2nd point
if(!firstClick) //EDIT: corrected
{
x2 = x;
y2 = y;
emit Mouse_Pressed();
firstClick = true;
}
}
}

ebsaith
10th June 2013, 13:42
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:


void my_qlabel::paintEvent(QPaintEvent *e)
{
if(paintFlag == 1)
{
QPainter painter(this);
QPen paintpen(Qt::red);
paintpen.setWidth(4);

QPen linepen(Qt::black);
linepen.setWidth(4);

QPoint p1;
p1.setX(x);
p1.setY(y);


QPoint p2;
p2.setX(x2);
p2.setY(y2);

painter.setPen(linepen);
painter.drawLine(p1, p2);

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

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

//paintFlag = 2;

}
update();
}


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

Kind Regards

Santosh Reddy
10th June 2013, 13:57
Post the complete code again

ebsaith
10th June 2013, 14:06
void my_qlabel::mousePressEvent(QMouseEvent *ev)
{
paintFlag = 0;
if(ev->button()==Qt::LeftButton)
{
this->x = ev->x();
this->y = ev->y();

//store 1st point
if(firstClick)
{
this->x = ev->x();
this->y = ev->y();
x1 = x;
y1 = y;
emit Mouse_Pressed();
firstClick = false;
}

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

paintFlag = 1;

//calculateAngle(x, y, x1, y2);
}
}


paint event:


void my_qlabel::paintEvent(QPaintEvent *e)
{
if(paintFlag == 1)
{
QPainter painter(this);
QPen paintpen(Qt::red);
paintpen.setWidth(4);

QPen linepen(Qt::black);
linepen.setWidth(4);

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

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

QPoint p2;
p2.setX(x2);
p2.setY(y2);

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

painter.setPen(linepen);
painter.drawLine(p1, p2);

}
update();
}


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

Santosh Reddy
10th June 2013, 14:11
Try this correction


void my_qlabel::mousePressEvent(QMouseEvent *ev)
{
paintFlag = 0;
if(ev->button()==Qt::LeftButton)
{
this->x = ev->x();
this->y = ev->y();

//store 1st point
if(firstClick)
{
this->x = ev->x();
this->y = ev->y();
x1 = x;
y1 = y;
emit Mouse_Pressed();
firstClick = false;
}

//Mouse Pressed Again! 2nd point
if(!firstClick)
{
x2 = x;
y2 = y;
emit Mouse_Pressed();
firstClick = true;
paintFlag = 1;
}
}
}

ebsaith
10th June 2013, 14:19
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?

Santosh Reddy
10th June 2013, 15:03
#include <QtGui>
#include <QtWidgets>
#include <QApplication>

class Label : public QLabel
{
public:
explicit Label(QWidget * parent = 0)
: QLabel(parent)
, mStartX(0)
, mStartY(0)
, mEndX(0)
, mEndY(0)
, mFirstClick(true)
, mPaintFlag(false)
{
;
}

protected:
void mousePressEvent(QMouseEvent * e)
{
if(e->button() == Qt::LeftButton)
{
//store 1st point
if(mFirstClick)
{
mStartX = e->x();
mStartY = e->y();
mFirstClick = false;
}
//Mouse Pressed Again! 2nd point
else if(!mFirstClick)
{
mEndX = e->x();
mEndY = e->y();
mFirstClick = true;
mPaintFlag = true;
update();
}
}
}

void paintEvent(QPaintEvent * e)
{
QLabel::paintEvent(e);

if(mPaintFlag)
{
QPainter painter(this);
QPen paintpen(Qt::red);
paintpen.setWidth(4);

QPen linepen(Qt::black);
linepen.setWidth(4);

QPoint p1;
p1.setX(mStartX);
p1.setY(mStartY);

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

QPoint p2;
p2.setX(mEndX);
p2.setY(mEndY);

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

painter.setPen(linepen);
painter.drawLine(p1, p2);
}
}

private:
int mStartX;
int mStartY;
int mEndX;
int mEndY;

bool mFirstClick;
bool mPaintFlag;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

Label w;
w.show();

return app.exec();
}

ebsaith
10th June 2013, 15:26
Thanks Santosh

Highly appreciated:)

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

Kind Regards

ebsaith
11th June 2013, 08:24
Good Day,

Once again I find myself in a stumbling block! :confused:

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?

Santosh Reddy
11th June 2013, 08:55
Call the base paintEvent.


void mmetLabel::paintEvent(QPaintEvent *ev)
{
QLabel::paintEvent(ev); //<<<<<<<<< Add this as first line
...
}

ebsaith
11th June 2013, 09:03
Works like a dream!
Thanks, Santosh.

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

Thanks again

Santosh Reddy
11th June 2013, 09:41
IMO Books provide you with content and may be examples, one becomes provicient by practice.

Anyway refer this Post (http://www.qtcentre.org/threads/54688-Well-can-anyone-list-some-book-Ishould-read?highlight=Books)

ebsaith
11th June 2013, 11:33
Kind Regards