benz6699
17th September 2014, 07:30
I am working on a feature in my GUI that allow user to draw polygon by clicking the points.
First the user needs to enable this function by clicking a button. After that the user clicks any four points on the screen. When the fourth point is clicked, all the 4 points will be connected to form a polygon.
However the polygon is not appearing after the fourth is clicked. Instead there is a message of "QPainter::begin: Paint device returned engine == 0, type: 1". What is the problem in my code?
bool QTGraphicsShape::eventFilter(QObject *obj, QEvent *event)
{
double static x[4],y[4];
int static i;
if ((event->type() == QEvent::GraphicsSceneMouseRelease) && (Draw3Points == true)) {
QGraphicsSceneMouseEvent *mouseEvent = static_cast< QGraphicsSceneMouseEvent* >( event );
QPointF img_coord_pt = mouseEvent->scenePos();
x[i] = img_coord_pt.x();
y[i] = img_coord_pt.y();
i++;
if (i >= 4)
{
Draw3Points = false;
i=0;
static const QPointF points[4] = {
QPointF(x[0], y[0]),
QPointF(x[1], y[1]),
QPointF(x[2], y[2]),
QPointF(x[3], y[3])
};
QPainter painter(this);
painter.drawPolygon(points, 4);
}
return true;
} else {
return QObject::eventFilter(obj, event);
}
}
void QTGraphicsShape::on_pushButton_clicked()
{
ui.graphicsView->setMouseTracking(true);
Draw3Points = true;
m_pGraphicsScene->installEventFilter(this);
}
First the user needs to enable this function by clicking a button. After that the user clicks any four points on the screen. When the fourth point is clicked, all the 4 points will be connected to form a polygon.
However the polygon is not appearing after the fourth is clicked. Instead there is a message of "QPainter::begin: Paint device returned engine == 0, type: 1". What is the problem in my code?
bool QTGraphicsShape::eventFilter(QObject *obj, QEvent *event)
{
double static x[4],y[4];
int static i;
if ((event->type() == QEvent::GraphicsSceneMouseRelease) && (Draw3Points == true)) {
QGraphicsSceneMouseEvent *mouseEvent = static_cast< QGraphicsSceneMouseEvent* >( event );
QPointF img_coord_pt = mouseEvent->scenePos();
x[i] = img_coord_pt.x();
y[i] = img_coord_pt.y();
i++;
if (i >= 4)
{
Draw3Points = false;
i=0;
static const QPointF points[4] = {
QPointF(x[0], y[0]),
QPointF(x[1], y[1]),
QPointF(x[2], y[2]),
QPointF(x[3], y[3])
};
QPainter painter(this);
painter.drawPolygon(points, 4);
}
return true;
} else {
return QObject::eventFilter(obj, event);
}
}
void QTGraphicsShape::on_pushButton_clicked()
{
ui.graphicsView->setMouseTracking(true);
Draw3Points = true;
m_pGraphicsScene->installEventFilter(this);
}