Results 1 to 2 of 2

Thread: keep QGraphicsItem away from other QGraphicitems

  1. #1
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default keep QGraphicsItem away from other QGraphicitems

    I have QGraphicsScene/QGraphicsView with some QGraphicItem items of specific type. I want to drag move them on the view and also keep them away of the others (check to not collide).
    In my subclass of QGraphicsItem is reimplemented itemChange to snap the newpos into my snap grid. In the same method I check if there is a collision with other items, and if so , i set the newpos to the oldpos before movement. This prevents my item to be dragged over another item, but when it collides, it remains stick with the other item, and I cannot pull it away of it.
    Any ideas how to implement it ?

    Qt Code:
    1. QVariant saItemSteelNode::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. double s;
    4. if (change == QGraphicsItem::ItemPositionChange) {
    5. QPointF newPos = value.toPointF();
    6. if(Settings::snapOn()==1)
    7. {
    8. s=Settings::snapSize();
    9. newPos.setX(floor(newPos.x()/s)*s);
    10. newPos.setY(floor(newPos.y()/s)*s);
    11. };
    12. //check collision
    13. saScene* m_scene=static_cast<saScene*>(scene());
    14. QList<QGraphicsItem*> sl=m_scene->collidingItems(this,Qt::IntersectsItemBoundingRect);
    15. qDebug()<<"newpos:"<<newPos<<" oldpos:"<<oldPos;
    16. if(sl.count()==0)//no collision
    17. {
    18. emit itemPositionChanged();
    19. }else //collision
    20. {
    21. newPos=oldPos;
    22. };
    23. oldPos=newPos;
    24. return newPos;
    25. };
    26. return value;
    27. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keep QGraphicsItem away from other QGraphicitems

    I solved the problem finally with the code:

    Qt Code:
    1. QVariant saItemSteelNode::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. double s;
    4. if (change == QGraphicsItem::ItemPositionChange) {
    5. QPointF newPos = value.toPointF();
    6. if(Settings::snapOn()==1)
    7. {
    8. s=Settings::snapSize();
    9. newPos.setX(floor(newPos.x()/s)*s);
    10. newPos.setY(floor(newPos.y()/s)*s);
    11. };
    12. //check collision with other nodes
    13. p.addEllipse(newPos,diameter/2.0+1,diameter/2.0+1);
    14. QList<QGraphicsItem*> sl=scene()->items(p);
    15. sl=saScene::refineItems(sl,saItemSteelNode::TypeSteelNode);
    16. sl.removeAll(this);
    17. if(sl.count()>0)//collision
    18. {
    19. newPos=pos();
    20. };
    21. emit itemPositionChanged();
    22. return newPos;
    23. };
    24. return QGraphicsItem::itemChange(change,value);
    25. return value;
    26. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Change QGraphicsScene Rect by dragging QGraphicItems
    By airglide in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2012, 18:36
  2. Replies: 7
    Last Post: 29th November 2010, 19:20
  3. Replies: 7
    Last Post: 12th March 2010, 12:40
  4. Replies: 2
    Last Post: 28th June 2008, 16:31
  5. Moving multiple QGraphicItems
    By StefanHirche in forum Newbie
    Replies: 2
    Last Post: 11th January 2007, 21:27

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.