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?
int sceneWidth = 800;
int sceneHeight = 600;
ui->graphicsView->setFixedWidth(sceneWidth);
ui->graphicsView->setFixedHeight(sceneHeight);
// mScene is a subclass of QGraphicsScene
mScene->clear();
if (!imageQt.isNull()) { // imageQt is QImage
QPixmap scaledImage
= QPixmap(originalImage.
scaledToHeight( (int) mScene
->height
(), Qt
::SmoothTransformation) );
ui->graphicsView->setFixedWidth( scaledImage.width() );
ui->graphicsView->setFixedHeight( scaledImage.height() );
mScene->addPixmap(scaledImage);
ui->graphicsView->setScene(mScene);
ui->graphicsView->setSceneRect(scaledImage.rect());
ui->graphicsView->fitInView(mScene->itemsBoundingRect() , Qt::KeepAspectRatio);
ui->graphicsView->show();
int sceneWidth = 800;
int sceneHeight = 600;
ui->graphicsView->setFixedWidth(sceneWidth);
ui->graphicsView->setFixedHeight(sceneHeight);
// mScene is a subclass of QGraphicsScene
mScene->clear();
if (!imageQt.isNull()) { // imageQt is QImage
QPixmap originalImage = QPixmap::fromImage(imageQt);
QPixmap scaledImage = QPixmap(originalImage.scaledToHeight( (int) mScene->height(), Qt::SmoothTransformation) );
ui->graphicsView->setFixedWidth( scaledImage.width() );
ui->graphicsView->setFixedHeight( scaledImage.height() );
mScene->addPixmap(scaledImage);
ui->graphicsView->setScene(mScene);
ui->graphicsView->setSceneRect(scaledImage.rect());
ui->graphicsView->fitInView(mScene->itemsBoundingRect() , Qt::KeepAspectRatio);
ui->graphicsView->show();
To copy to clipboard, switch view to plain text mode
Bookmarks