[Qt-interest] QGraphicsRectItem::boundingRect() bug/fix?

Samuel Rødal samuel.rodal at nokia.com
Wed Jan 27 08:37:18 CET 2010


ext Malyushytsky, Alex wrote:
> I've got a problem in QT 4.6, which seems as a bug to me.
> 
> For some reason QGraphicsRectItem::boundingRect() assumes that d->rect  is normalized,
> which is not correct when width / heights are negative.
> This makes such items not being properly updated (in most cases not updated at all, unless you force scene to update that area in other way.
> 
> In my case I was able to fix the problem by overriding QGraphicsRectItem::boundingRect() as in below:
> 
> QRectF UbGraphicsRectItem::boundingRect() const
> {
>          return QGraphicsRectItem::boundingRect().normalized();
> }
> 
> But this breaks the QGraphicsRectItem cashing of bounding rectangle, which makes the code slower and might be incorrect if pen width is set.
> 
> The right thing probably would be to fix  QGraphicsRectItem::boundingRect() as below:
> 
> QRectF QGraphicsRectItem::boundingRect() const
> {
>     Q_D(const QGraphicsRectItem);
>     if (d->boundingRect.isNull()) {
>         qreal halfpw = pen().widthF() / 2;
> //        d->boundingRect = d->rect; // to replace
>         d->boundingRect = d->rect.normalized(); // fix
>         if (halfpw > 0.0)
>             d->boundingRect.adjust(-halfpw, -halfpw, halfpw, halfpw);
>     }
>     return d->boundingRect;
> }
> 
> I would report the bug, but not sure this was not intended.
> Anybody could comment on this?

I believe the current behavior is intended, as the docs specify that 
rendering of invalid (as defined by QRectF::isValid()) rectangles is 
undefined: http://qt.nokia.com/doc/4.6/qgraphicsrectitem.html#details

Basically you should make sure your rectangles are normalized before 
passing them to QGraphicsRectItem.

--
Samuel



More information about the Qt-interest-old mailing list