Example of re-sizing a QGraphicsItem using Mouse events.

Download the complete project zip file

screen shot of demo app

This example adds the ability for the user to re-size the box by dragging the corners.

Setting the viewport mode of the QGraphicsView turned out to be required to avoid some serious rendering artifacts. If the user drags the mouse rapidely, shrinking the size of the box, then the areas of the scene that were once covered by the box’s paiter are notupadted, leaving artifacts.

screen shot of artifacts

The solution is to set the viewport’s rendering mode to QGraphicsView::BoundingRectViewportUpdate. Otherwise, the default setting is QGraphicsView::MinimalViewportUpdate, which attempts make things runs faster by only updating rectangles that have been flagged for update.

 

 

This box can be re-sized and it can be moved. For moving, we capture the mouse move events and move the box location accordingly.

To resize the box, we place small indicator boxes on the four corners that give the user a visual cue to grab with the mouse. The user then drags the corner to stretch or shrink the box. The corner grabbers are implemented with the CornerGrabber class.
The CornerGrabber class captures the mouse when the mouse is over the corner’s area, but the StateBox object (which owns the corners) captures and processes the mouse events on behalf of the CornerGrabbers (because the owner wants to be resized, not the CornerGrabbers themselves). This is accomplished by installing a scene event filter on the CornerGrabber objects:
_corners[0]->installSceneEventFilter(this);

 

When the box detects mouse hover events, the CornerGrabbers are installed,
and the Scene Event Filter is installed for each corner. This allows the owner object to handle
the mouse capture events on behalf of the CornerGrabbers- their function in life is to give
the user a visual indication of the corner and then to tell the owner that the user has click
on the corner.

1 Comment Add your own

  • 1. Toby Speight  |  October 7, 2011 at 9:32 am

    Perhaps calling prepareGeometryChange() would eliminate that drawing artefact? I think that’s what it is for.

Leave a Reply

Trackback this post  |  Subscribe to the comments via RSS Feed