How to get the visible rect of a QGraphicsView ?
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.
Re: How to get the visible rect of a QGraphicsView ?
Am not sure, but you can try QWidget::visibleRegion
Also have a look at QAbstractScrollArea::viewportHope it helps :)
Re: How to get the visible rect of a QGraphicsView ?
If you haven't rotated your scene/view
Code:
QRectF scenerect
= QRectF(mapToScene
(0,
0), mapToScene
(width
(), height
()));
should work. Otherwise
should return the trapezoid of the viewRect.
Re: How to get the visible rect of a QGraphicsView ?
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"):
Code:
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(overviewedWidget
->horizontalScrollBar
()->value
(),
overviewedWidget->verticalScrollBar()->value())));
Probably, I don't need to request the viewport, but can work with the overviewed widget itself.
Greets.