Results 1 to 9 of 9

Thread: GraphicsView: Notifying geometry change to parent group

  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default GraphicsView: Notifying geometry change to parent group

    Hi,
    I have custom QGraphicsTextItem whose instances are added to a QGraphicsItemGroup subclass. The textitem has TextEditorInteraction as textinteraction flags.

    Now whenever the size of the textitem changes due to text entered by keyboard the group doesn't seem to update its childrenRect and hence when the group is selected later , the selected rect remains smaller and the some part of text of text item remains outside the selected rect.
    For now i have this hack

    Qt Code:
    1. void PropertyText::focusOutEvent(QFocusEvent *event)
    2. {
    3. QGraphicsTextItem::focusOutEvent(event);
    4. trimText();
    5. if(group()) {
    6. group()->addToGroup(dummy);
    7. group()->removeFromGroup(dummy);
    8. delete dummy;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Can this be regarded as a bug in Qt ? Also are there any better/efficient workarounds ?

    I'm using Qt-4.3 - x11
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView: Notifying geometry change to parent group

    You're correct, it seems like the itemsBoundingRect in QGraphicsItemGroupPrivate is updated only when adding/removing items from the group.

    Couldn't you adjust the geometry of the group after you finish editing the inner text item?
    I think a union would suffice : groupBounds = groupBounds | textItemBounds.

    Regards

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView: Notifying geometry change to parent group

    Even better, I think you can assign QGraphicsItem::childrenBoundingRect to the group's geometry directly. No additional calculations needed.
    That is how they do it. Well, not exactly like that :
    Qt Code:
    1. void QGraphicsItemGroup::addToGroup(QGraphicsItem *item)
    2. {
    3. if (!item) {
    4. qWarning("QGraphicsItemGroup::addToGroup: cannot add null item");
    5. return;
    6. }
    7. if (item == this) {
    8. qWarning("QGraphicsItemGroup::addToGroup: cannot add a group to itself");
    9. return;
    10. }
    11.  
    12. QTransform oldSceneMatrix = item->sceneTransform();
    13. item->setPos(mapFromItem(item, 0, 0));
    14. item->setParentItem(this);
    15. item->setTransform(oldSceneMatrix
    16. * sceneTransform().inverted()
    17. * QTransform().translate(-item->x(), -item->y()));
    18. item->d_func()->setIsMemberOfGroup(true);
    19. prepareGeometryChange();
    20. d->itemsBoundingRect |= (item->transform() * QTransform().translate(item->x(), item->y()))
    21. .mapRect(item->boundingRect() | item->childrenBoundingRect());
    22. update();
    23. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, the entire geometry update is done in one line.

    Regards

  4. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: GraphicsView: Notifying geometry change to parent group

    But the itemsBoundRect variable of the group is private and isn't accesible at all. So how do i assign the children's bounding rect to groups item bound rect?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView: Notifying geometry change to parent group

    Override its boundingRect function and always return childrenBoundingRect().
    Well, you can cache this value(for speed purposes, if you have a lot of objects in the group) and invalidate it, I don't know, for example when the text item's geometry changes.

    Anyway, by default QGraphicsItemGroup::boundingRect returns the d->itemsBoundingRect from QGrahicsItemGroupPrivate.

    This should work.

    Regards.

  6. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: GraphicsView: Notifying geometry change to parent group

    Yeah caching is needed. I'll see which hack amongst these to use.
    But can this be regarded as qt bug ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView: Notifying geometry change to parent group

    Quote Originally Posted by Gopala Krishna View Post
    Yeah caching is needed. I'll see which hack amongst these to use.
    But can this be regarded as qt bug ?
    I don't know. Pretty hard to say. Looking at the Qt sources it doesn't seem like something was forgotten or misplaced.
    It appears more like it was intended behavior. At least to me.

    I wonder if Illustrator or InDesign resize their groups when their children's geometry changes. I am not at work now, so I cannot say for sure.
    But I can remember pretty well that they do.

    Regards

  8. #8
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: GraphicsView: Notifying geometry change to parent group

    Thanks marcel for your insights
    I'd say adding a method to recalculate boundingrect to the itemgroup will do the trick.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: GraphicsView: Notifying geometry change to parent group

    Unfortunately this won't be fixed even in future versions of qt
    task tracker
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

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.