PDA

View Full Version : Problem with QPainter



bhavya
3rd March 2011, 13:06
Hi everyone,

I am using Qpainter to paint on a pixmap by using mouse movements

The following problems were there -

1. QPainter::begin: Paint device returned engine == 0, type: 2

2. While drawing the coordinate system gets screwed up and there is a shift while drawing the lines.

Here is my part of source code..
Scaled is the pixmap I scaled to (300,300) after loading the image


void MainWindow::mousePressEvent(QMouseEvent *event)
{

if (event->button() == Qt::LeftButton) {

lastPoint = event->globalPos();
scribbling = true;
}
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && scribbling)

drawLineTo(event->globalPos());
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(&scaled);
QRect dirtyRect = event->rect();
painter.drawPixmap(dirtyRect, scaled, dirtyRect);
}
void MainWindow::drawLineTo(const QPoint &endPoint)
{
QPainter painter(&scaled);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
//painter.begin(&scaled);
painter.drawLine(lastPoint, endPoint);


int rad = (myPenWidth / 2) + 2;
update(QRect(lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
lastPoint = endPoint;
//painter.end();
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && scribbling) {
drawLineTo(event->globalPos());
scribbling = false;
}
ui->Image -> setPixmap (scaled);
}

high_flyer
3rd March 2011, 13:08
You are not allowed to draw out side a paint event.
You have to change your design such, that all drawing happens only in the paintEvent().
One why would be to store the coordinates during the mouse events and in the mouse event call update() which it turn will call paintEvent(), in which you can then use the saved coordinates to draw.