我想用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,同樣的問題...
也許我非常非常疲倦,但我真的不明白什麼是錯的。請幫助我:)
我覺得OP在想itemsBoundingRect()來解決所有的RECT場景(唯一的)項:HTTP://doc.qt .digia.com/qt/qgraphicsscene.html#itemsBoundingRect – jdi
我試試你的解決方案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
好吧另一件事.. 。我在MainWindow中嘗試這最後一個代碼。它效果很好。我在QWidget中嘗試tgis代碼,它在單擊MainWindow上的按鈕後顯示,並且不起作用:我有相同的結果:http://images.meteociel.fr/im/6981/result_hnx1.PNG – QLag