问题描述
我想在QGraphicsView中使用QGraphicsScene显示图像。我的代码非常基本:
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());
我的问题是我的图片尺寸错误。它非常小,比例错误,位于GraphicsView的中心。使用qDebug,我知道我的图片加载成功,它的大小约为100x800px。我的graphicsView较小,所以我想调整我的图片大小来调整我的GraphicsView大小。
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.
在主窗口Form中设置graphicsView,并在标题中声明场景:QGraphicsScene scene;
The graphicsView is set in the mainwindow Form and the scene is declared in the header : "QGraphicsScene scene;"
我尝试在世界上一切皆有可能(我认为)但是graphicsView是空白的或包含图片的小版本。当我复制/粘贴一些互联网代码时,我总是遇到这个问题。我也尝试这个例子:,同样的问题......
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 :)
推荐答案
sceneRect()
可能不是什么你认为除非你专门设置它。此外,您在 fitInview
调用中缺少 aspectRatioMode
,这可能会扭曲图像,有时会导致小外观。
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);
这篇关于QGraphicsView / QGraphicsScene图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!