Get visible rectangle of QGraphicsView?

前端 未结 6 1465
借酒劲吻你
借酒劲吻你 2020-12-15 22:04

I\'ve been pulling my hair out with this one for hours. There\'s a thread here about it, but nothing seems to be working. QGraphicsView::rect() will return the width and hei

6条回答
  •  我在风中等你
    2020-12-15 22:46

    The following implementation returned the best results for me:

    QRectF getVisibleRect( QGraphicsView * view )
    {
        QPointF A = view->mapToScene( QPoint(0, 0) ); 
        QPointF B = view->mapToScene( QPoint( 
            view->viewport()->width(), 
            view->viewport()->height() ));
        return QRectF( A, B );
    }
    

    This works still really well when scrollbars appear. This only works properly if the view does not display the scene rotated or sheared. If the view is rotated or sheared, then the visible rectangle is not axis parallel in the scene coordinate system. In this case

    view->mapToScene( view->viewport()->geometry() )
    

    returns a QPolygonF (NOT a QRectF) which is the visible rectangle in scene coordinates. By the way, QPolygonF has a member function boundingRect() which does not return the properly visible rectangle of the view, but might be useful anyways.

提交回复
热议问题