PDA

View Full Version : How to add multiple line using qpainter



athulms
16th August 2011, 11:14
i want to draw 12separate line over a grid. using mouse clicks.
When 1 click on over the click a point is to be shown then for the next click a line should be drawn to the current point from the first point. On third click another point is to be created. and on fourth click a line should appear from the third point to the fourth point.
I used the code as.

constructor:- i=0;

void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::LeftButton)
{
point = e->pos();
if(k==0)
{
point2=point;
k=1;
update();
return;
}
if(k==1)
k=0;
update();
}
}
void MainWindow::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setWidth(25);
linepen.setCapStyle(Qt::RoundCap);
painter.setRenderHint(QPainter::Antialiasing,true) ;
painter.setPen(linepen);
if(k==1)
painter.drawPoint(point);
else
painter.drawLine(point,point2);
}




the line is drawn correctly but on the third click the first line will disappear. so i can draw only a single line. give me a solution thanks in advance

marcvanriet
16th August 2011, 12:41
Hi,
Now you remember only 2 points where the user clicks. You have to remember all 4 points where the user clicks and draw the line between the points that were clicked.

You might want to learn about arrays too.

Regards,
Marc

athulms
16th August 2011, 15:36
k, I will try