1

I'm trying to move a QGraphicsItem with the mouse hover over the parent item.

BaseItem::BaseItem(const QRectF &bounds) : theBounds(bounds), theMousePressed(false) { theLineItem = new LineItem(theBounds, this); setAcceptHoverEvents(true); } 

and

void BaseItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { QPointF position = mapToScene( event->pos()); theLineItem->setPos( position); } 

But the item is not moving. Is there any other way to move the item on the scene with mouse move, without using the ItemIsMovable flag, because I want the item to be moved around the parent item, once it's invoked?

1 Answer 1

1

When you create the LineItem, in its constructor you pass the BaseItem as the parent.

A call to setPos on a GraphicsItem, sets the position of the item relative to its parent which, in this case, is the BaseItem.

Mapping the event->pos() to scene coordinates is wrong here. event->pos() returns the position in local coordinates of the receiving object, which in this case is the BaseItem.

Therefore, you should be setting the position of theLineItem directly with event->pos().

theLineItem->setPos(event->pos()); 

Note that if you did happen to want the event position in scene coordinates, there's a function already available: -

event->scenePos(); 

So you would not have needed to call mapToScene.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.