0

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!

t0bias
  • 93
  • 3
  • 13
  • I don't know about Qt, but check out SetCapture and ClipCursor in the Win32 API. –  Feb 14 '17 at 00:17
  • http://stackoverflow.com/questions/11172420/moving-object-with-mouse maybe override mouseMoveEvent for that purpose? – Hafnernuss Feb 14 '17 at 06:15
  • *drag a QGraphicsItem out of the scenes viewport* - A QGraphicsItem lives in a QGraphicsScene, you can't drag it out of the scene's viewport. – TheDarkKnight Feb 14 '17 at 08:55

1 Answers1

1

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.

m. c.
  • 867
  • 5
  • 12