how to Fit QGraphicsScene in a QGraphicsView

view->setSceneRect(0,0,view->frameSize().width(),view->frameSize().height());

Connect this piece of code with resizeevent of Qgraphicsview

If you set scene rect with size greater than of views scroll bars will appear. Else if you set scen rect equal to views frame width or less, no scroll bars will be there.

Then I guess QGraphicsView::fitInView() is your solution.:)


This worked for me:

void MyGraphicsView::zoomToFit()
{
    fitInView(scene()->sceneRect(), Qt::KeepAspectRatio);
}

You might want to adjust the scene rectangle to have a little margin; might look better, depending on your content.


scaling the view like bellow doing what required:

view->scale(frameWidth / sceneWidth, frameHeight / sceneHeight);

Tags:

Qt4