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:

Qt Code:
  1. class MyGIGroupObject : public QObject, public QGraphicsItemGroup
  2. {
  3. Q_OBJECT
  4. public:
  5. MyGIGroupObject( QGraphicsItem * parent = 0 ) : QGraphicsItemGroup(parent)
  6. {
  7. }
  8. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  9. {
  10. qDebug() << "MyGIGroupObject paint called";
  11. painter->setPen(Qt::red);
  12. painter->drawRect(boundingRect());
  13. }
  14. void addToGroup ( QGraphicsItem * item )
  15. {
  16. qDebug() << "before width=" << QGraphicsItemGroup::boundingRect().width() << "height=" << QGraphicsItemGroup::boundingRect().height();
  17. QGraphicsItemGroup::addToGroup(item);
  18. qDebug() << "after width=" << QGraphicsItemGroup::boundingRect().width() << "height=" << QGraphicsItemGroup::boundingRect().height();
  19. }
  20. };
To copy to clipboard, switch view to plain text mode 


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