Results 1 to 8 of 8

Thread: QGraphicsItem::ItemIgnoresTransformations and children

  1. #1

    Default QGraphicsItem::ItemIgnoresTransformations and children

    Hi,

    I'm using a QGraphicsView to display a scene comprised of a tree of QGraphicsItem nodes where leaves are geometric objects (with area) and parents (without area) are represented as selectable pins. The view can be zoomed in and out. I would like the pin to keep the same size in the viewport regardless of the zoom transform but still allow the pin children (the geometric objects) to be zoomed. I tried to set the ItemIgnoresTransformations flag on the pins but unfortunately, it seems this flag is recursively set to all the pins' children so the zoom is no longer being applied to geometric objects.

    Is there a way to ignore a transform on a given QGraphicsItem (for display and selection purpose) but still pass down the transform to the its children?

    Thanks,
    PM

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem::ItemIgnoresTransformations and children

    You can make the pin a sibling of the tree root and use a non-visible item as their shared parent.

    Cheers,
    -

  3. #3

    Default Re: QGraphicsItem::ItemIgnoresTransformations and children

    Sounds good. Thanks for the suggestion. However, a detail I omitted to mention in my previous post is that my application allows the pins to be moved around and the geometric children underneath must follow suit. Consequently, if I put the pin as a child of the tree node, the transformation of the tree node needs to mirror the transformation of the pin child while the pin child itself stays centered on the tree node. What is the proper way to achieve this?

    Regards,
    PM

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem::ItemIgnoresTransformations and children

    You can try implementing itemChange() so that it reacts to ItemPositionChange, but instead of accepting the value returns its current position and moves the parent instead.

    Cheers,
    _

  5. #5

    Default Re: QGraphicsItem::ItemIgnoresTransformations and children

    I followed your idea of re-implementing itemChanged() to intercept ItemPositionChanged events and let the pins communicate their new positions back to their parent:

    Qt Code:
    1. QVariant PinItem::itemChange(GraphicsItemChange change, const QVariant& value) {
    2. if (change == QGraphicsItem::ItemPositionChange) {
    3. parentItem()->setPos(value.toPointF());
    4. return pos();
    5. }
    6. return value;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Obviously, since the pin is centered on the parent origin and the parent is given the pin position, it is warped back to the scene origin every time a new move operation is initiated. A naive solution would be to add the new pin position to the parent position rather than replace it:

    Qt Code:
    1. QVariant PinItem::itemChange(GraphicsItemChange change, const QVariant& value) {
    2. if (change == QGraphicsItem::ItemPositionChange) {
    3. parentItem()->setPos(parentItem()->pos() + value.toPointF());
    4. return pos();
    5. }
    6. return value;
    7. }
    To copy to clipboard, switch view to plain text mode 

    But this doesn't work as intended because the parent position is then accumulated at every step of the translation which accelerate it away from the scene origin. Any suggestion?

    Thanks for your help,
    PM

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem::ItemIgnoresTransformations and children

    How about calculating the difference between pos and value and adding that to parent's pos?

    Cheers,
    _

  7. #7

    Default Re: QGraphicsItem::ItemIgnoresTransformations and children

    By "pos", I assume you meant the pin position relative to its parent? In my case, the pin position is always (0, 0) aka centered on its parent origin. Subtracting it from "value" would behave just like the second piece of code in my last post.

    Another test I've done is to let the pin acquire "value" as its new position and do the subtraction you mentioned. Doing so, the parent follow the mouse cursor as intended but the pin moves away from its parent origin.

    Thanks,
    PM

  8. #8

    Default Re: QGraphicsItem::ItemIgnoresTransformations and children

    After banging my head against the wall a few times, I finally managed to find a solution. It's slightly devious but it does in fact achieve the behavior I was initially aiming for. Rather than using the itemChange() callback to communicate ItemPositionChange, I use it to communicate ItemSelectedChange. More precisely, every time a pin is selected, I select it's parent instead. Subsequently, when a translation is initiated, the parent follows the cursor and the pin isn't moved relatively to its parent, as intended.

    Thanks again for your help!
    PM

Similar Threads

  1. Replies: 4
    Last Post: 21st November 2012, 14:05
  2. Replies: 2
    Last Post: 9th September 2011, 09:57
  3. Replies: 2
    Last Post: 6th February 2010, 16:31
  4. Problem with QGraphicsView and ItemIgnoresTransformations
    By coderCPP1981 in forum Qt Programming
    Replies: 6
    Last Post: 27th June 2008, 05:28
  5. QGV: ItemIgnoresTransformations and RubberBand
    By Vladimir in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2007, 14:38

Tags for this Thread

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.