Results 1 to 9 of 9

Thread: have anyone used collidesWithItem?

  1. #1
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default have anyone used collidesWithItem?

    I am displaying multiple QGraphicsTextItem in a scene. all QGraphicsTextItem sets flag
    QGraphicsTextItem::ItemIsMovable .
    I want QGraphicsTextItem should not overlapped with other QGraphicsTextItem while moving QGraphicsTextItem using mouse.
    any idea how to do that?
    is there any example how to use QGraphicesItem::collidesWithItem()?



  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: have anyone used collidesWithItem?

    I think you need to override the drag events for the items.
    While drag move, u need to check if its colliding with other items, and if colliding, set its position to previous safe position.

    I had implemented a case where I restricted a item within the scene rectangle. Hope it might help u

  3. #3
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: have anyone used collidesWithItem?

    can you send that code to restricted a item within the scene rectangle. it may help me.

  4. #4
    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: have anyone used collidesWithItem?

    Not drag events but mouseMoveEvent.

  5. #5
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: have anyone used collidesWithItem?

    I have written the following but its not working.
    Qt Code:
    1. void GraphicsTextItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    2. {
    3. QPointF pt = pos();
    4. QGraphicsTextItem::mouseMoveEvent ( mouseEvent );
    5. QList<QGraphicsItem *> list = collidingItems() ;
    6. if(list.count() > 0)
    7. {
    8. setPos(pt);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 5th March 2007 at 23:43. Reason: changed [html] to [code]

  6. #6
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: have anyone used collidesWithItem?

    hi,

    i'm having the same problem.
    And came up with the same (not working) solution. It is not working because the collidingItems-functions doesn't work for an item that is currenlty being moved. If I drop the item over another one I cant remove it form there (so the code is actually working)

    any other ideas?

    niko

  7. #7
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: have anyone used collidesWithItem?

    You want to override QGraphicsItem::itemChange() and check for ItemPositionChange. Just like the example in the docs.

  8. #8
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Cool Re: have anyone used collidesWithItem?

    The best and most efficient approach is to use QGraphicsItem::itemChange(). That way you can avoid moving the item forward and backward. Also by reimplementing this function, you can snap up any other attempt to move the item, not just mouseMove() but also setPos(), and you can even intercept transformation changes (which can also affect the item's position). Here's one way to stop an item from being moved if it collides with another (untested code):

    Qt Code:
    1. QVariant MyItem::itemChange(GraphicsItemChange change, const QVariant &val)
    2. {
    3. if (change == ItemPositionChange) {
    4. QPainterPath sceneShape = mapToParent(shape()).translate(val.toPointF());
    5. if (QGraphicsItem *parent = parentItem())
    6. sceneShape = parent->mapToScene(sceneShape);
    7. if (scene()->items(sceneShape, Qt::IntersectsItemShape).size() > 1) {
    8. // Collision! Don't move.
    9. return pos();
    10. }
    11. }
    12. return QGraphicsItem::itemChange(change, val);
    13. }
    To copy to clipboard, switch view to plain text mode 

    This is a bit crude, but I hope you get the picture. Map your shape to your parent coordinates, translate it by your future position (provided through the val-argument), and map that to the scene if necessary. Now ask the scene if any item other than your own collides with that shape. If so, return the old pos instead of the new one to effectively abort the repositioning.

    It's crude, because 1) you really want to check if the list of items is empty, or contains only your item (not just that it contains at most 1 item). And 2) because instead of just not moving the item, it's common to neatly position the item at the point of intersection / right next to the other. That's hard to do in general, so I'll leave that up to the app writer to figure out ;-).
    Last edited by Bitto; 25th September 2007 at 21:15. Reason: typo
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  9. The following user says thank you to Bitto for this useful post:

    niko (26th September 2007)

  10. #9
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: have anyone used collidesWithItem?

    much thanks - this is the code that works for me:
    Qt Code:
    1. QPainterPath path = shape();
    2.  
    3. QMatrix matrix;
    4. matrix.translate(value.toPointF().x(), value.toPointF().y());
    5. polygon = matrix.map(path);
    6.  
    7. QList<QGraphicsItem*> items = scene()->items(path, Qt::IntersectsItemShape);
    8. items.removeAll(this);
    9.  
    10. if (!items.isEmpty()) {
    11. // Collision! Don't move.
    12. return pos();
    13. }
    To copy to clipboard, switch view to plain text mode 

    i didn't need the mapToParent and mapToScene calls. furthermore QPainterPath doesn't have a translate-function afaics - so i used qmatrix for that...

    niko

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.