I want to display an image in a QGraphicsView with a QGraphicsScene. My code is very basic :
QGraphicsScene* scene = new QGraphicsScene(this);
scene->addPixmap(QPixmap(qApp->applicationDirPath() +"/france.gif"));
ui->myGraphicsView->setScene(scene);
ui->myGraphicsView->fitInView(scene->sceneRect());
My problem is that my picture has a wrong size. It is very small, with wrong proportions, and in the center of myGraphicsView
.
With qDebug
, I know that my picture is loaded successfully, and it has a size of about 100x800px. my GraphicsView
is smaller, so I want to resize my picture to adjust it with myGraphicsView
size.
The graphics view is set in the mainwindow
form, and the scene is declared in the header: QGraphicsScene scene;
.
I tried many things, but the graphics view is always blank, or contains the small version of the picture.
I also tried this example: Qt GUI Development - Displaying a 2D grid using QGraphicsView, but same problem.
sceneRect()
may not be what you think it is unless you specifically set it. Also you are missing aspectRatioMode
in fitInview
call which can distort it image, sometimes resulting in "small" appearance.
QGraphicsScene* scene = new QGraphicsScene(this);
QGraphicsPixmapItem p = scene->addPixmap(QPixmap(qApp->applicationDirPath() +"/france.gif"));
ui->myGraphicsView->setScene(scene);
ui->myGraphicsView->fitInView(p, Qt::KeepAspectRatio);