I am using QGraphicsView/QGraphicsScene to display an image. The image is always displayed to its original size with scroll bars at the ends. I want QGraphicsView to fit the image automatically as per the the size of the window keeping aspect ratio.
I tried this but nothing happened:
ui->graphicsView->fitInView(0,0,ui->graphicsView->width(),ui->graphicsView->height(),Qt::KeepAspectRatio);
You are providing the rectangle of the view and not that of the scene.
This should work:
itemsBoundingRectcalculates and returns the bounding rect of all items on the scene. So the graphicsview’s view matrix will be scaled in order to fit the contents of the scene.I would advise you to reimplement
resizeEventand have this call there as well.