PDA

View Full Version : Unadjusted bouding box with setCosmetic(true)



smegy
6th January 2015, 16:21
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:


QMatrix vMatrix;
vMatrix.scale(HorizontalScale(), VerticalScale());
setMatrix(vMatrix);
scene()->update();

And the segment that way:


class Segment : public QGraphicsView {

qreal mX1;
qreal mY1;
qreal mX2;
qreal mY2;

void paint(QPainter* aPainter, const QStyleOptionGraphicsItem* aOption, QWidget* aWidget) {
QPen vPen(QColor(0, 0, 255));
vPen.setCosmetic(true);
vPen.setWidth(3);

QPaintDevice *vDevice = aPainter->device();
aPainter->setPen(vPen);
aPainter->drawLine(mX1, mY1, mX2, mY2);
}

QRectF boundingRect() const {
qreal vHalfWidth = sPen.width() / 2.0f;
qreal vX = qMin(mX1, mX2) - vHalfWidth;
qreal vY = qMin(mY1, mY2) - vHalfWidth;
qreal vWidth = qAbs(mX2 - mX1) + vHalfWidth*2;
qreal vHeight = qAbs(mY2 - mY1) + vHalfWidth*2;

return QRectF(vX, vY, vWidth, vHeight);
}
}

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 ?