I want to display "basic" segments in a QGraphicsView with fixed width for the segments, zoom in/out feature and efficient bouding boxes.

The zoom is implemented that way:

Qt Code:
  1. QMatrix vMatrix;
  2. vMatrix.scale(HorizontalScale(), VerticalScale());
  3. setMatrix(vMatrix);
  4. scene()->update();
To copy to clipboard, switch view to plain text mode 

And the segment that way:

Qt Code:
  1. class Segment : public QGraphicsView {
  2.  
  3. qreal mX1;
  4. qreal mY1;
  5. qreal mX2;
  6. qreal mY2;
  7.  
  8. void paint(QPainter* aPainter, const QStyleOptionGraphicsItem* aOption, QWidget* aWidget) {
  9. QPen vPen(QColor(0, 0, 255));
  10. vPen.setCosmetic(true);
  11. vPen.setWidth(3);
  12.  
  13. QPaintDevice *vDevice = aPainter->device();
  14. aPainter->setPen(vPen);
  15. aPainter->drawLine(mX1, mY1, mX2, mY2);
  16. }
  17.  
  18. QRectF boundingRect() const {
  19. qreal vHalfWidth = sPen.width() / 2.0f;
  20. qreal vX = qMin(mX1, mX2) - vHalfWidth;
  21. qreal vY = qMin(mY1, mY2) - vHalfWidth;
  22. qreal vWidth = qAbs(mX2 - mX1) + vHalfWidth*2;
  23. qreal vHeight = qAbs(mY2 - mY1) + vHalfWidth*2;
  24.  
  25. return QRectF(vX, vY, vWidth, vHeight);
  26. }
  27. }
To copy to clipboard, switch view to plain text mode 

With the code above and an horizontal segment. The bouding box height is always returned to 3. But when zoomin, the segment always has its 3 pixels width (thanks to setCosmetic(true)) BUT the bounding box is stretched larger than the segment drawing.

Any idea on how to handle this ?