Results 1 to 8 of 8

Thread: Re-Implementation of QGraphics::itemChange() NOT working

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re-Implementation of QGraphics::itemChange() NOT working

    hi guys, I have a graphicsItem that is a child item of another graphicsItem which is basically a filled rectangle. the child item is movable and I want to limit the movement of the child item to the bounds of the parent item. The bounding rect of the parent is the same size as the scene rect hence I used the scene rect to define the bounds in the code below. But my code below doesn't work and the child item still goes upside the parent item bounds when you drag move it. I want the item to stop at the scene bounds or the parent item bounds (which are the same rect size). please see code below.
    Qt Code:
    1. QVariant Picture::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
    2. {
    3. if(change == ItemPositionChange && scene()){
    4. QPointF newPos = value.toPointF();
    5. QRectF rect2 = scene()->sceneRect();
    6.  
    7. if(!rect2.contains(newPos)){
    8. // Keep the item inside the scene rect.
    9. newPos.setX(qMin(rect2.right(), qMax(newPos.x(), rect2.left())));
    10. newPos.setY(qMin(rect2.bottom(), qMax(newPos.y(), rect2.top())));
    11.  
    12. return newPos;
    13. }
    14.  
    15. }
    16. return QGraphicsItem::itemChange(change, value);
    17. }
    To copy to clipboard, switch view to plain text mode 

    thanking you in advance.

  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: Re-Implementation of QGraphics::itemChange() NOT working

    The parent's rectangle from a child's point of view is (0, 0, parent.width, parent.height).

    Alternatively you can map your position into scene coordinates.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Re-Implementation of QGraphics::itemChange() NOT working

    Quote Originally Posted by anda_skoa View Post
    The parent's rectangle from a child's point of view is (0, 0, parent.width, parent.height).

    Alternatively you can map your position into scene coordinates.

    Cheers,
    _
    I am not sure if I am doing this right but I have done this
    Qt Code:
    1. return mapToScene(newPos);
    To copy to clipboard, switch view to plain text mode 
    to mapToScene coordinates and this doesn't work either. But may have done something wrong. any suggestions?

  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: Re-Implementation of QGraphics::itemChange() NOT working

    Quote Originally Posted by ayanda83 View Post
    I am not sure if I am doing this right but I have done this
    Qt Code:
    1. return mapToScene(newPos);
    To copy to clipboard, switch view to plain text mode 
    to mapToScene coordinates and this doesn't work either. But may have done something wrong. any suggestions?
    How would that help?

    As I said, you can either do the check in the item's coordinate space or in the scene's coordinate space.
    Mixing the two is not likely to have the correct results.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Re-Implementation of QGraphics::itemChange() NOT working

    Damn! this has proven to be more difficult than I thought. Three days later and I am still struggling with this problem.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Re-Implementation of QGraphics::itemChange() NOT working

    What is so difficult about it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Re-Implementation of QGraphics::itemChange() NOT working

    Whats difficult about it is that I still cannot keep the child item within the bounds of the parent item (i.e. when I drag move the child item, I don't want it to go beyond the bounding rect of the parent item). I was convinced that the code I posted above will do the trick, but guess I was wrong because it doesn't work.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Re-Implementation of QGraphics::itemChange() NOT working

    anda_skoa told you it wouldn't do the trick because you are mixing two coordinate spaces.

    Quote Originally Posted by ayanda83
    I want to limit the movement of the child item to the bounds of the parent item
    Bounds of the parent item:
    Qt Code:
    1. QRectF bounds = parent->boundingRect();
    To copy to clipboard, switch view to plain text mode 
    Limit movement of child to that rect:
    Qt Code:
    1. if(!bounds.contains(pos())) { ... }
    To copy to clipboard, switch view to plain text mode 

    Riddle:
    What coordinate space are we using here?
    a) item
    b) item's parent
    c) scene
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 25th January 2012, 18:28
  2. Child GraphicsItems Not Getting itemChange()
    By jawbreak in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2010, 02:26
  3. itemChange method
    By zgulser in forum Qt Programming
    Replies: 4
    Last Post: 17th August 2009, 09:19
  4. Moving Items with itemChange?
    By konvex in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2008, 14:36
  5. itemChange() glibc invalid pointer
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 3rd May 2008, 09:04

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.