draw points on mouse click shows error painter not active.
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.
Re: draw points on mouse click shows error painter not active.
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.
Re: draw points on mouse click shows error painter not active.
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
Re: draw points on mouse click shows error painter not active.
Of course it shows the same error,as you have not corrected it
Quote:
Originally Posted by
athulms
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);
}
}
Re: draw points on mouse click shows error painter not active.
ya i have used the update(); method. now it worked