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?

Qt Code:
  1. bool QTGraphicsShape::eventFilter(QObject *obj, QEvent *event)
  2. {
  3. double static x[4],y[4];
  4. int static i;
  5. if ((event->type() == QEvent::GraphicsSceneMouseRelease) && (Draw3Points == true)) {
  6. QGraphicsSceneMouseEvent *mouseEvent = static_cast< QGraphicsSceneMouseEvent* >( event );
  7. QPointF img_coord_pt = mouseEvent->scenePos();
  8. x[i] = img_coord_pt.x();
  9. y[i] = img_coord_pt.y();
  10. i++;
  11. if (i >= 4)
  12. {
  13. Draw3Points = false;
  14. i=0;
  15. static const QPointF points[4] = {
  16. QPointF(x[0], y[0]),
  17. QPointF(x[1], y[1]),
  18. QPointF(x[2], y[2]),
  19. QPointF(x[3], y[3])
  20. };
  21. QPainter painter(this);
  22. painter.drawPolygon(points, 4);
  23. }
  24. return true;
  25. } else {
  26. return QObject::eventFilter(obj, event);
  27. }
  28. }
  29.  
  30. void QTGraphicsShape::on_pushButton_clicked()
  31. {
  32. ui.graphicsView->setMouseTracking(true);
  33. Draw3Points = true;
  34. m_pGraphicsScene->installEventFilter(this);
  35. }
To copy to clipboard, switch view to plain text mode