PDA

View Full Version : Updating the boundingRect of a QGraphicsItem



robin2000
14th August 2009, 08:17
hi,

i have a QGraphicsItemGroup with several items in it. The boundingRect is computed correctly according to the current boundingRects of the group members. Now i change the size of one member in the group, so that his boundingRect exceeds the group's boundingRect.
But when I call group.boundingRect(), it was not updated. Is there any way to manually update the boundingRect of an existing item group? Even implementing an inherited version of boundingRect() in a child class of QGraphicsItemGroup does not work, because boundingRect in QGraphicsItemGroup is not virtual thus the graphics engine would only call the ancestors version, when treating the object as a QGraphicsItem.
The only workaround i found is to remove an item from the group and adding it again, because then the boundingRect stored in the private variable d->itemsBoundingRect is updated. But that can't be the solution.

I'd be grateful for any ideas.

wysota
14th August 2009, 08:27
Sure it's virtual...

robin2000
14th August 2009, 08:32
No, boundingRect() is only virtual in QGraphicsItem not in QGraphicsItemGroup. Here is the code from the implementation:

/*!
\reimp

Returns the bounding rect of this group item, and all its children.
*/
QRectF QGraphicsItemGroup::boundingRect() const
{
Q_D(const QGraphicsItemGroup);
return d->itemsBoundingRect;
}



and the declaration in class QGraphicsItemGroup:

1062 QRectF boundingRect() const;

wysota
14th August 2009, 08:38
#include <iostream>
class A {
public:
virtual void function() { std::cout << "A" << std::endl; }
};

class B : public A {
public:
void function() { std::cout << "B" << std::endl; }
};

class C : public B {
void function() { std::cout << "C" << std::endl; }
};

int main(){
A *obj = new A;
obj->function();
delete obj;
obj = new B;
obj->function();
delete obj;
obj = new C;
obj->function();
delete obj;
return 0;
}

robin2000
14th August 2009, 08:47
Your so right. It didn't work in my case, because I tried to call QGraphicsItemGroup.boundingRect() instead of QGraphicsItem.boundingRect(). But that's easy to solve now.

Thank you very much for your fast help. Learned something about inheritance :D