PDA

View Full Version : how to draw points with qpainter?



athulms
19th August 2011, 09:10
void MainWindow::mousePressEvent(QMouseEvent *f)
{
point=f->pos();
y=1;
update();
}

void MainWindow::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setCapStyle(Qt::RoundCap);
linepen.setWidth(30);
painter.setRenderHint(QPainter::Antialiasing,true) ;
painter.setPen(linepen);
if(y==1)
painter.drawPoint(point);

}


i have used the code. but draws only single point. when i click to draw a second point the first point will disappear. at a time only one point will appear..

stampede
19th August 2011, 09:29
Its because paintEvent clears the widget before painting, so everything previously painted is gone.
You should try using a QList of points to store every clicked point and paint all of them.

athulms
19th August 2011, 10:35
k i will try to do it. thanks

Added after 29 minutes:


void MainWindow::mousePressEvent(QMouseEvent *e)
{
point=e->pos();
update();
}
void MainWindow::paintEvent(QPaintEvent *e)
{
setAttribute(Qt::WA_OpaquePaintEvent);
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setCapStyle(Qt::RoundCap);
linepen.setWidth(30);
painter.setRenderHint(QPainter::Antialiasing,true) ;
painter.setPen(linepen);
painter.drawPoint(point);
}


it worked for no need for list

high_flyer
19th August 2011, 10:43
it worked for no need for list
That is only because you are only using your dedicated painting in your paint event.
But what if you want the standard painting AND your painting?
Or if you cover your widget with another window, and then show your widget again?
Then you will only get the last point.
What stampede suggested is the correct way.

athulms
23rd August 2011, 08:10
k thanks i will do it with list