I found a solution (feel free to post my alternatives :)), yet I thought this answer might be useful, as I struggled to work with Google and the documentation for 15 hours.
The key should not only call fitInView (), but also setSceneRect (). This did it for me (replace FooBar with your own class name):
void FooBar::resizeEvent(QResizeEvent *) { fitView(); } void FooBar::showEvent(QShowEvent *) { fitView(); } void FooBar::fitView() { const QRectF rect = QRectF(-0.5,-0.5, 1, 1); ui->graphicsView->fitInView(rect, Qt::KeepAspectRatio); ui->graphicsView->setSceneRect(rect); }
Sebastian mach
source share