Reputation: 133
I would like to retain the ability of a user to zoom and drag a QGraphicsScene
, thus I cannot simply lock the QGraphicsView
.
However a user should not be able to drag a QGraphicsItem
out of the scenes viewport. Therefore I am looking for a way to interrupt a MouseDragEvent without ignoring the DragMoveEvent (aka have the QGraphicsItem
jump back to its origin point). I have tried to accomplish this behaviour using the releaseMouse()
-function but that didn't work at all. Any suggestions?
Thanks!
Upvotes: 0
Views: 284
Reputation: 895
When dealing with qt graphics scene view frame work and dragging, it is better to re-implement QGraphicsItemand::itemChange than dealing with mouse directly.
This is the function defined in header file:
protected:
virtual QVariant itemChange( GraphicsItemChange change, const QVariant & value );
Then in the function, you detect the position change, and return the new position as needed.
QVariant YourItemItem::itemChange(GraphicsItemChange change, const QVariant & value )
{
if ( change == ItemPositionChange && scene() )
{
QPointF newPos = value.toPointF(); // check if this position is out bound
{
if ( newPos.x() < xmin) newPos.setX(xmin);
if ( newPos.x() > xmax ) newPos.setX(xmax);
if ( newPos.y() < ymin ) newPos.setY(ymin);
if ( newPos.y() > ymax ) newPos.setY(ymax);
return newPos;
}
...
}
Something like this, you get the idea.
Upvotes: 1