PDA

View Full Version : QGraphicsItemGroup boundRect problem with transformed children



qlands
3rd March 2011, 09:24
Hi,

I have a problem creating a group from a series of items if one of them has a transformation matrix.

I add the items by:



for (int pos2 = 0; pos2 <= tnkItems.count() -1;pos2++)
{
group->addToGroup(tnkItems[pos2]);
}


If the items do not have any transformation group->boundingRect() return the total area of its children. But if any of its items has a transformation matrix , for example scale(2, 1 ), group->boundingRect() return an area bigger than its child items.

Any idea why?

Many thanks,
Carlos.

MarekR22
3rd March 2011, 11:12
See QGraphicsItem::boundingRect it clearly says:

...
Although the item's shape can be arbitrary, the bounding rect is always rectangular, and it is unaffected by the items' transformation.
...
So group->boundingRect() should return always the same value independently of own transformation.
BUT if children transformation changes then group->boundingRect() can change (in case of one item MUST change) since area occupied by the child (in coordinates of group) is changed when transformation is applied on child.
It is like changing area of painting.

qlands
4th March 2011, 12:06
Hi MarekR22

I traced the problem to this:

The scene has QGraphicsPolygonItems that act as containers for QGraphicsProxyWidgets.

The ProxyWidget fill the size of its container polygon. The proxy Ignores Transformations. If the container gets transformed for example scale(2,1), I change the size of the widget in the proxy to fill the container with this code:



float w2;
float h2;
w2 = container->sceneBoundingRect().width();
h2 = container->sceneBoundingRect().height();
widgetOfProxy->setGeometry(0,0,w2,h2);


After this the size of the proxy is twise its original size to fill the area of the container.

If I create a group using this object the resulting group is twice the observable size.

You can access an example of this here:
www.qlands.com/other_files/test.tar.gz


Many thanks for your help
Carlos.

qlands
7th March 2011, 08:46
I'm replying to my own post.

This happens because the bounding rectangle of the group is calculated by summing the bounding rectangle of all its children using childrenBoundingRect() and mapping the result rectangle to the group. Because the childrenBoundingRect reach the widget and the the widget size can change, this results in a erroneous bounding rectangle for the group.

I fixed this by overriding the boundingRect() and paint functions.

I hope this would help others with the same problem.

Closed thread.

Carlos.