PDA

View Full Version : setRect vs boundingRect



zgulser
4th February 2009, 13:52
Hi,

I got confused with the boundingRect. Since setRect sets the coodinate system of the corresponding item, what is boundingRect used for. I mean only setRect seems enough. What is the difference?

Regards

jpn
4th February 2009, 14:33
But setRect() is not part of any QGraphicsItem but just QGraphicsRectItem (and QGraphicsEllipseItem) where it makes perfect sense.

zgulser
4th February 2009, 14:44
Hi again,

Yeah I mean QGraphicsRectItem or QGraphicsEllipseItem in which setRect method exists. But if we once make item.setRect(...), don't we already have a bounding rect?

Regards

jpn
4th February 2009, 16:04
Yeah I mean QGraphicsRectItem or QGraphicsEllipseItem in which setRect method exists. But if we once make item.setRect(...), don't we already have a bounding rect?
What do you mean? QGraphicsRectItem uses that rect (adjusted with pen width) as bounding rect. But other items in general don't have any setRect() method. The bounding rect is used by the graphics scene.

zgulser
4th February 2009, 22:56
More clearly;

For example, QGraphicsEllipseItem.setRect() = QGraphicsEllipseItem.boundingRect() ?

Regards

wysota
5th February 2009, 00:23
Looking at the source code is a good way to answer your question (although J-P has already answered it a couple of times):

QRectF QGraphicsRectItem::boundingRect() const
{
Q_D(const QGraphicsRectItem);
if (d->boundingRect.isNull()) {
qreal halfpw = pen().widthF() / 2;
d->boundingRect = d->rect;
if (halfpw > 0.0)
d->boundingRect.adjust(-halfpw, -halfpw, halfpw, halfpw);
}
return d->boundingRect;
}
Especially take a look at line #6.

d->rect is set by QGraphicsRectItem::setRect().