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);
}
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);
}