QGraphicsView / QGraphicsScene image size

2019-07-29 07:39发布

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 on the center of my GraphicsView. With qDebug i kwow that my picture is load successfully and it has a size of about 100x800px. My graphicsView is smaller so i want to resize my picture to adjust it with my GraphicsView size.

The graphicsView is set in the mainwindow Form and the scene is declared in the header : "QGraphicsScene scene;"

I try everything is possible in the world (i think) but the graphicsView is alaways blank or contains the small version of the picture. When I copy/paste some codes of internet i always get this problem. I also try with this example : Qt GUI Development - Displaying a 2D grid using QGraphicsView , same problem...

Maybe i'm very very very tired but i really don't understand what is wrong. Help me please :)

2条回答
Summer. ? 凉城
2楼-- · 2019-07-29 07:51

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);
查看更多
该账号已被封号
3楼-- · 2019-07-29 08:03

From my experience, a shown image is very small if we try to fitInView before the scene window is displayed. Technically, the sceneRect size is not set in the way we want until it is actually shown. Therefore, we need to let the scene window shows up before we can properly use fitInView.

Well, I think the solution is available here Qt5 C++ QGraphicsView: Images don't fit view frame

查看更多
登录 后发表回答