View Full Version : Interacting QGraphicsItems together

16th April 2011, 07:27
Hey Guys,

I currently have a graphics scene with dynamic QGraphicsItems being added to the scene. I would like to snap these items together by dragging one item on top of another and releasing the mouse button.

In the QGraphicsItem class i have the following function

public void dragEnterEvent(QGraphicsSceneDragDropEvent event)
System.out.println("Dragged on the item");

This code works fine when I drag some item from my desktop (say an Excel file), but it doesn't detect any item in my scene beign dragged on top of it.

Any help would really be appreciated.


16th April 2011, 07:58
Why are you using drag events and not mouse move events?

16th April 2011, 09:35
I thought I could use DragEvents....initially i tried dealing with mouseMove

If i used a mouse move event and detect the itemAt a particularpoint, it always returns the item i'm currently dragging (top most item).

If I wanted it to snap on top of another QGraphicsItem, can I use the mouseReleaseEvent in the QGraphicsScene


public void mouseReleaseEvent(QGraphicsSceneMouseEvent event)
//Code here to see if the item currently being dragged is on top of another item

Added after 1 3 minutes:

Found the solution. ColodingItems...

Block newBlock;
List l1 = collidingItems(selectedBlock);
for (int i = 0; i < l1.size(); i++)
newBlock = (Block)l1.get(i);