PDA

View Full Version : How to get the visible rect of a QGraphicsView ?



shad0w
16th October 2008, 12:19
Hi there,

I want to retrieve the currently visible rectangle of a QGraphicsView (Qt 4), since by zooming in and scolling around that visible region is obviously changed and doesn't match the scene rect anymore (this is going to be used to simply visualize the currently visible region in some overview widget - like the similar function in Photoshop or other image editing programs).

Is there any API method like QGraphicsView::fitInView which seems to be the opposite of what I'm trying to do? For instance, I know that there was QWidget::visibleRect() in Qt 3 but not in Qt 4. Maybe there is some replacement?

Currently, I'm trying to manually calculate that visible rect by the scroll bar positions and the current scale factor - but not yet successfully.

Thanks.

aamer4yu
17th October 2008, 05:37
Am not sure, but you can try QWidget::visibleRegion

Also have a look at QAbstractScrollArea::viewport
Hope it helps :)

spud
19th October 2008, 11:08
If you haven't rotated your scene/view

QRectF scenerect = QRectF(mapToScene(0,0), mapToScene(width(), height()));
should work. Otherwise

QPolygonF trapezoid = mapToScene (rect());
should return the trapezoid of the viewRect.

shad0w
21st October 2008, 11:41
Hi,

ok, after having a second thought, this was easy - thanks to your replies. I did somthing like this (let's suppose, we are in the "overviewing widget"):



QMatrix const matrix = overviewedGraphViewWidget->matrix().inverted();
QRect visibleRect = matrix.mapRect(overviewedWidget->viewport()->rect());

// when the viewport is scrolled, then the top left point of the visible rectangle needs to be moved
// according to the scroll bar positions
visibleRect.moveTopLeft(matrix.map(QPoint(overview edWidget->horizontalScrollBar()->value(),
overviewedWidget->verticalScrollBar()->value())));


Probably, I don't need to request the viewport, but can work with the overviewed widget itself.

Greets.