Important: Please read the Qt Code of Conduct -

Event handling in a Graphics Scene behaving weirdly

  • I am going to break this post into four parts to explain my problem.

    1. My goal I want to achieve.

    2. My logic to achieve it.

    3. My implementation (code) of the logic.

    4. And the unexplained deviation way from my expected behavior/goal.

    5. Goal. (to learn how event handling works really)

    I have two pictures in a PixmapItem on GraphicsScene. The item flags have been set to "ItemIsMovabe"
    I want to know whether there is a collision when a user moves one picture over the other.
    Moreover I want to know if there is a collision only when the user releases the mouse after dragging one picture over the other.

    1. Description of my algorithm/logic.
      When the user releases mouse after dragging an object check if the object has any items it has collided with it then respond.
      Sublclass GraphicsPixMapItem and reimplement mouseRelease event handler so that I can respond to a mouse release event.
      get list of colliding items with the item being released.
      if the list isn't empty then there was a collision.
      print to screen the message "collison on mouse release"

    2. My implementation.

    @ class MyPixmapItem : public QGraphicsPixmapItem{
    MyPixmapItem(const QPixmap & pixmap):QGraphicsPixmapItem(pixmap){}
    void mouseReleaseEvent(QGraphicsSceneMouseEvent event){
    > list = this->collidingItems();
    if (!list.isEmpty()){
    qDebug() << "collision on mouse release";

    1. Unexpected behavior.
    • No mater where I release a picture after dragging it there always is a collision detected even if the one picture is miles apart from the other. So after releasing the mouse the message "collision on release" is always printed to the console.
    • when I try and move a item/picture by clicking on it and dragging, it suddenly disappears and instantly reappears at the center of the scene. It allows me to move the picture but not before it has done its magical act and the mouse is not even over the picture when I am moving it.

    I have mulled over this for a while now and I can't explain it.
    Can you?...Explain it...that is.

Log in to reply