2012-10-13 125 views
1

我想用QGraphicsScene在QGraphicsView中显示图像。我的代码是非常基本的:QGraphicsView/QGraphicsScene image size

QGraphicsScene* scene = new QGraphicsScene(this); 
scene->addPixmap(QPixmap(qApp->applicationDirPath() +"/france.gif")); 
ui->myGraphicsView->setScene(scene); 
ui->myGraphicsView->fitInView(scene->sceneRect()); 

我的问题是,我的图片有错误的大小。它非常小,比例错误,位于GraphicsView的中心。随着qDebug我kwow,我的图片加载成功,它的大小约100x800px。我的graphicsView更小,所以我想调整我的图片来调整我的GraphicsView大小。

graphicsView在主窗口窗体中设置,场景在标头中声明:“QGraphicsScene scene;”

我尝试一切都可能在世界上(我认为),但graphicsView是alaways空白或包含图片的小版本。当我复制/粘贴一些互联网代码时,我总是会遇到这个问题。我也试着用这个例子:Qt GUI Development - Displaying a 2D grid using QGraphicsView,同样的问题...

也许我非常非常疲倦,但我真的不明白什么是错的。请帮助我:)

回答

1

sceneRect()可能不是你认为它是,除非你专门设置它。你也错过aspectRatioModefitInview调用它可以扭曲它的形象,有时导致“小”的外观。

QGraphicsScene* scene = new QGraphicsScene(this); 
QGraphicsPixmapItem p = scene->addPixmap(QPixmap(qApp->applicationDirPath() +"/france.gif")); 
ui->myGraphicsView->setScene(scene); 
ui->myGraphicsView->fitInView(p, Qt::KeepAspectRatio); 
+0

我觉得OP在想itemsBoundingRect()来解决所有的RECT场景(唯一的)项:HTTP://doc.qt .digia.com/qt/qgraphicsscene.html#itemsBoundingRect – jdi

+0

我试试你的解决方案Stephen: 'QPixmap pixmap(qApp-> applicationDirPath()+“/ france.gif”); QGraphicsPixmapItem * item = scene.addPixmap(pixmap); ui-> myGraphicsView-> setScene(&scene); ui-> myGraphicsView-> fitInView(item,Qt :: KeepAspectRatio);' 现在我得到这个结果: http://images.meteociel.fr/im/6981/result_hnx1.PNG – QLag

+0

好吧另一件事.. 。我在MainWindow中尝试这最后一个代码。它效果很好。我在QWidget中尝试tgis代码,它在单击MainWindow上的按钮后显示,并且不起作用:我有相同的结果:http://images.meteociel.fr/im/6981/result_hnx1.PNG – QLag

0

根据我的经验,如果我们在显示场景窗口之前尝试fitInView,则显示的图像非常小。从技术上讲,sceneRect尺寸没有按照我们想要的方式设置,直到实际显示。因此,我们需要让场景窗口显示出来,然后才能正确使用fitInView。

嗯,我认为该解决方案可在这里 Qt5 C++ QGraphicsView: Images don't fit view frame