Hi,

I’m trying to scale a image to fit in the QGraphicView. Sometimes it works, but for example, if I load a image of size 1500×900, the resized image is cut a bit in the right border, even with the scrool activated. What is wrong in the code below?

Qt Code:
  1. int sceneWidth = 800;
  2. int sceneHeight = 600;
  3.  
  4. ui->graphicsView->setFixedWidth(sceneWidth);
  5. ui->graphicsView->setFixedHeight(sceneHeight);
  6.  
  7. // mScene is a subclass of QGraphicsScene
  8. mScene->clear();
  9.  
  10. if (!imageQt.isNull()) { // imageQt is QImage
  11.  
  12. QPixmap originalImage = QPixmap::fromImage(imageQt);
  13. QPixmap scaledImage = QPixmap(originalImage.scaledToHeight( (int) mScene->height(), Qt::SmoothTransformation) );
  14.  
  15. ui->graphicsView->setFixedWidth( scaledImage.width() );
  16. ui->graphicsView->setFixedHeight( scaledImage.height() );
  17.  
  18. mScene->addPixmap(scaledImage);
  19. ui->graphicsView->setScene(mScene);
  20. ui->graphicsView->setSceneRect(scaledImage.rect());
  21. ui->graphicsView->fitInView(mScene->itemsBoundingRect() , Qt::KeepAspectRatio);
  22.  
  23. ui->graphicsView->show();
To copy to clipboard, switch view to plain text mode