2012-10-13 169 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