Important: Please read the Qt Code of Conduct -

QGraphicsRectItem - mouseMoveEvent setting the rect /capture Control modifier when using itemChange

  • I have a QGraphicsRectItem within a QGraphicsScene, which should be movable within the scene and to help achieve the behavior I 've set the following flags within the QGraphicsRectItem object's constructor and defined itemChange and mouseMoveEvent.

    @ setFlags(QGraphicsItem::ItemIgnoresParentOpacity|
    #if QT_VERSION >= 0x40804

    #if 0
    QVariant GVHelper::itemChange(GraphicsItemChange change, const QVariant &value)
      if (change == ItemPositionHasChanged)
      return QGraphicsRectItem::itemChange(change,value);
    void GVHelper::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
      Qt::KeyboardModifiers mod = event->modifiers();
      if ((event->buttons() & Qt::LeftButton) && (mod & Qt::ControlModifier))
        printf("Newposition x= %lf y = %lf\n", newPos.x(), newPos.y());
        printf("Items position x= %lf y = %lf\n", itemNode()->position().x(), itemNode()->position().y());

    While itemChange alone works perfectly fine, since the need was to move the item only when the CTRL key is been held down, I had to implement the mouseMoveEvent

    1. Is there a way to continue to use ItemChange, but capture the mouse/Control key modifiers?

    2. With mouseMoveEvent, QGraphicsRectItem new position is based on the mouse-cursor which doesn't account for the boundingRect etc and it doesn't look quite as smooth as itemChange(). Is there a better way to account for the QRectF when using mouseMoveEvent's position?

Log in to reply