Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, August 2007
Using QGraphicsView::fitInView in rotated view


Message 1 in thread

Hi,

I am using QGraphicsView in Qt 4.2 (in the process of installing 4.3).
If the user rubber-band drags using a zoom tool, I want to zoom the
view to best fit the selected area. The method
QGraphicsView::fitInView() is the obvious method to call. I can pass
it the rubber-band drag rectangle, after calling mapToScene() to
obtain the area in view coordinates. This works fine if the scene is
not rotated. Of course, if the scene is rotated, the selection
"rectangle" is really a polygon, whose bounds are not what I want (the
bounds are too large).

This is the insufficient way:
QPolygonF selectionPoly = mapToScene(selectionRect);
fitInView(QRectF(selectionPoly.boundingRect());
// The bounding rect is much larger than the selection rect if the
view is rotated.

It works as expected, but of course is not sufficient for the UI to
feel right because the zoom fit is not tight on the selection area.
After trying my own QRegion intersection manipulation which can only
result in a boundingRect anyway, I realized that there is already an
overloaded fitInView() that can take a QGraphicsPolygonItem. I define
the item from the selectionPoly. The documentation says "Ensures that
item fits tightly inside the view...". However, the behavior I see is
exactly as if it is also simply getting the polygon's boundingRect,
which is of course larger than the selection rect when the view is
rotated. It doesn't fit tightly to the polygon item.

This is what I expected to work, but which has the same effect:
QPolygonF selectionPoly = mapToScene(selectionRect);
item = new QGraphicsPolygonItem(selectionPoly, ...);
fitInView(item);

Is there a better way to take the rubber band drag rectangle and ask
the QGraphicsView to best fit it, even when rotated? There is
certainly some simple math that can be applied to the view using the
unmapped mouse coordinates, but I don't know if that is the best way
to go.

Thanks,
Trygve Isaacson

--
 [ signature omitted ]