PDA

View Full Version : QGraphicsItemGroup::addToGroup miscalculates bounding rect



jonks
14th December 2009, 15:59
I've derived a class from QGraphicsItemGroup because I want to paint a border around the QGraphicsItemGroup.
My class looks like this. I've made all the functions inline for brevity of this post:


class MyGIGroupObject : public QObject, public QGraphicsItemGroup
{
Q_OBJECT
public:
MyGIGroupObject( QGraphicsItem * parent = 0 ) : QGraphicsItemGroup(parent)
{
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
qDebug() << "MyGIGroupObject paint called";
painter->setPen(Qt::red);
painter->drawRect(boundingRect());
}
void addToGroup ( QGraphicsItem * item )
{
qDebug() << "before width=" << QGraphicsItemGroup::boundingRect().width() << "height=" << QGraphicsItemGroup::boundingRect().height();
QGraphicsItemGroup::addToGroup(item);
qDebug() << "after width=" << QGraphicsItemGroup::boundingRect().width() << "height=" << QGraphicsItemGroup::boundingRect().height();
}
};



I overrode the addToGroup function to track down a problem where my border is not being drawn. I've been pretty confused by the results.

I add two QGraphicsItems of the same dimensions to the group using the addToGroup function, which logs the change in the size of QGraphicsItemGroup bound rect.
I'm finding some very odd behaviour of QGraphicsItemGroup addToGroup depending on whether I step though the application in the debugger (F5), or if I run it (Ctrl-R)

When I use the debugger (F5) I get this output, which is correct and the border is drawn correctly:


before width= 0 height= 0
after width= 117 height= 136
before width= 117 height= 136
after width= 196 height= 140
MyGIGroupObject paint called


When I use Ctrl-R I always get this output, which is very different:


before width= 0 height= 0
after width= 5.48613e+303 height= 136
before width= 5.48613e+303 height= 136
after width= 5.48613e+303 height= 137

Notice the widths are completely wrong. The heights are correct.
In this case, the border is NOT drawn (my the paint function is not called).
However, the items added to the group are drawn.

Does anyone have any idea what the difference could be between executing using F5 and Ctrl-R, and why that difference might break my app? (My application is single threaded btw).

I upgraded from 4.5 to 4.6 a couple of weeks ago. I'm pretty sure this used to work a couple of months ago under 4.5, but I cannot be sure - It's been a long time since I tested this particular functionality.

Is there an alternative way to get a border drawn around a QGraphicsItemGroup object?
- am I doing this the hard way?!

Thanks
- Jonks

jonks
19th December 2009, 16:46
Well I just discovered that I had added 2 child QGraphicsItems to one of my objects.

Even when the children are hidden, they're are included in the group's bounding rect calculation. (smells like a bug in Qt)

The two children had uninitialized dimension data, which causes the a crazy result.
(the data is only initialized when the child objects are set to visible)

I guess gdb auto-initializes all uninitialized data, which would explain why the cause of the problem cannot be seen when stepping through with the debugger.

I hope this finding helps other people out there.