Hi,

I am trying to add some custom QGraphicsItems in a QGraphicsScene on mouse click and at mouse cursor coordinates. But the items are not added at the same coordinates as the mouse cursor's.

Qt Code:
  1. renderArea::renderArea(QWidget *parent):
  2. QGraphicsView(parent)
  3. {
  4. scene = new QGraphicsScene(this);
  5. scene->setItemIndexMethod(QGraphicsScene::NoIndex);
  6. scene->setSceneRect(0, 0, 850, 480);
  7. setScene(scene);
  8. setCacheMode(CacheBackground);
  9. setViewportUpdateMode(BoundingRectViewportUpdate);
  10. setRenderHint(QPainter::Antialiasing);
  11. setTransformationAnchor(AnchorUnderMouse);
  12. scale(qreal(1.0), qreal(1.0));
  13. setMinimumSize(400, 400);
  14. }
  15.  
  16. void renderArea::mousePressEvent(QMouseEvent *event)
  17. {
  18. QPoint p = event->pos();
  19.  
  20. updateList(p);
  21. }
  22.  
  23. void renderArea::updateList(const QPoint &p)
  24. {
  25. Point point;
  26. point.point = p;
  27. point.isSelected = false;
  28. list.append(point);
  29. if (list.size() > 1)
  30. updateClothoid(list[list.size()-2].point, list[list.size()-1].point);
  31. }
  32.  
  33. void renderArea::updateClothoid(const QPoint &p1, const QPoint &p2)
  34. {
  35. Clothoid *temp = new Clothoid(p1, p2);
  36.  
  37. clothoids.append(temp);
  38.  
  39. scene->addItem(temp);
  40. }
To copy to clipboard, switch view to plain text mode 

renderArea being the QGraphicsView and Clothoids the custom QGraphicsItem

Qt Code:
  1. Clothoid::Clothoid(QPoint startPoint, QPoint endPoint)
  2. {
  3. sPoint = startPoint;
  4. ePoint = endPoint;
  5. startCurvature = 0.0;
  6. endCurvature = 0.0;
  7. clothoidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) +
  8. pow(endPoint.y() - startPoint.y(),2));
  9. }
  10.  
  11. QRectF Clothoid::boundingRect() const
  12. {
  13. qreal penWidth = 1;
  14.  
  15. if ((sPoint.x() &lt ePoint.x()) && (sPoint.y() &lt ePoint.y()))
  16. return QRectF(sPoint.x(), sPoint.y(), ePoint.x() - sPoint.x(), ePoint.y()-sPoint.y())
  17. .normalized()
  18. .adjusted(-penWidth, -penWidth, penWidth, penWidth);
  19.  
  20. if ((sPoint.x() &lt ePoint.x()) && (sPoint.y() > ePoint.y()))
  21. return QRectF(sPoint.x(), ePoint.y(), ePoint.x() - sPoint.x(), sPoint.y() - ePoint.y())
  22. .normalized()
  23. .adjusted(-penWidth, -penWidth, penWidth, penWidth);
  24.  
  25. if ((sPoint.x() > ePoint.x()) && (sPoint.y() &lt ePoint.y()))
  26. return QRectF(ePoint.x(), sPoint.y(), sPoint.x() - ePoint.x(), ePoint.y()-sPoint.y())
  27. .normalized()
  28. .adjusted(-penWidth, -penWidth, penWidth, penWidth);
  29.  
  30. if ((sPoint.x() > ePoint.x()) && (sPoint.y() > ePoint.y()))
  31. return QRectF(ePoint.x(), ePoint.y(), sPoint.x() - ePoint.x(), sPoint.y() - ePoint.y())
  32. .normalized()
  33. .adjusted(-penWidth, -penWidth, penWidth, penWidth);
  34.  
  35. return QRectF();
  36.  
  37. }
  38.  
  39. void Clothoid::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
  40. {
  41. QLineF line(sPoint, ePoint);
  42.  
  43. // Draw the line itself
  44. painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
  45. painter->drawLine(line);
  46. }
To copy to clipboard, switch view to plain text mode 

I am guessing that the coordinates to which I am inserting the items belong to the GraphicsView and not the scene as in my application the scene doesn't cover the entire view. But how could I get the coordinates of the scene in my case?