kaa
kaa

Reputation: 1367

Why QGraphicsItem::ItemIsMovable has no effect?

I just want to add movable label to the scene:

QGraphicsWidget* w = scene->addWidget( new QLabel( "Test Label" ) );
w->setFlag( QGraphicsItem::ItemIsMovable, true );

but label stays unmovable.

Upvotes: 2

Views: 3158

Answers (1)

TheDarkKnight
TheDarkKnight

Reputation: 27639

If the item is not set as selectable, it will also not be able to be moved.

w->setFlag(QGraphicsItem::ItemIsSelectable, true);

In addition, as you're adding a widget to the scene, the widget is embedded in a QGraphicsProxyWidget. As the documentation states: -

It forwards events between the two objects and translates between QWidget's integer-based geometry and QGraphicsWidget's qreal-based geometry.

So the mouse events that would normally be handled by the QGraphicsItem are being propagated to the widget and this doesn't know that it is in a proxy widget. In order to drag the widget around the scene, you'd need to change this so that the QGraphicsProxyWidget handles the events.

One method would be to derive from QGraphicsProxyWidget and override the mouseMoveEvent, mousePressEvent and mouseReleaseEvent. Then detect whether the mouse is clicked and released, without dragging, in which case propagate the event to the widget, else if you detect that the mouse has moved in the move event, without a call to mouseReleaseEvent, then move the proxy widget accordingly and set the event as accepted with QEvent::accept()

Upvotes: 3

Related Questions