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|
QGraphicsItem::ItemIsMovable|
#if QT_VERSION >= 0x40804
QGraphicsItem::ItemSendsScenePositionChanges|
#endif
QGraphicsItem::ItemIsSelectable);#if 0 QVariant GVHelper::itemChange(GraphicsItemChange change, const QVariant &value) { if (change == ItemPositionHasChanged) { newPos.setX(value.toPointF().x()); newPos.setY(value.toPointF().y()); itemNode()->setPositionNoGraphicsItem(newPos); } return QGraphicsRectItem::itemChange(change,value); } #endif void GVHelper::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { Qt::KeyboardModifiers mod = event->modifiers(); if ((event->buttons() & Qt::LeftButton) && (mod & Qt::ControlModifier)) { newPos.setX(event->scenePos().x()); newPos.setY(event->scenePos().y()); 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()); itemNode()->setPositionNoSceneUpdate(newPos); } }@
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
-
Is there a way to continue to use ItemChange, but capture the mouse/Control key modifiers?
-
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?
-