PDA

View Full Version : draw points on mouse click shows error painter not active.



athulms
12th August 2011, 12:21
void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::LeftButton)
{
QPoint point = e->pos();
int x=point.rx();
int y=point.ry();
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setWidth(30);
linepen.setCapStyle(Qt::RoundCap);
painter.setRenderHint(QPainter::Antialiasing,true) ;
painter.setPen(linepen);
painter.drawPoint(x,y);
}
}


What is the error in the code. Waiting for rply.

MasterBLB
12th August 2011, 12:34
The reason of the error is using QPainter not inside paintEvent().Move the drawing to the paintEvent(),here in mousePressEvent just save points to draw on some list or so.

athulms
12th August 2011, 12:45
void MainWindow::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setWidth(30);
linepen.setCapStyle(Qt::RoundCap);
painter.setRenderHint(QPainter::Antialiasing,true) ;
painter.setPen(linepen);
}


void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::LeftButton)
{
QPoint point = e->pos();
int x=point.rx();
int y=point.ry();
QPainter painter(this);
painter.drawPoint(x,y);
}
}


This also shows the same error

MasterBLB
12th August 2011, 13:01
Of course it shows the same error,as you have not corrected it

void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::LeftButton)
{
QPoint point = e->pos();
int x=point.rx();
int y=point.ry();
QPainter painter(this);//this is fckig wrong.QPainter instances may not be created outside the paintEvent
painter.drawPoint(x,y);
}
}

athulms
12th August 2011, 13:20
ya i have used the update(); method. now it worked