Results 1 to 2 of 2

Thread: QGraphicsItemGroup::addToGroup miscalculates bounding rect

  1. #1
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QGraphicsItemGroup::addToGroup miscalculates bounding rect

    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

  2. #2
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Talking Re: QGraphicsItemGroup::addToGroup miscalculates bounding rect

    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.

Similar Threads

  1. QGraphicsSvgItem, problem with scaling (invalid bounding rect)
    By Józef Paczenko in forum Qt Programming
    Replies: 3
    Last Post: 8th June 2010, 09:58
  2. How to get Rect or Size by QStyle?
    By nifei in forum Qt Programming
    Replies: 8
    Last Post: 21st November 2008, 10:09
  3. Replies: 3
    Last Post: 12th July 2008, 23:51
  4. hide bounding rect
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 8th May 2008, 10:40
  5. Image format without bounding rect
    By dreamer in forum Qt Programming
    Replies: 5
    Last Post: 30th April 2008, 23:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.