Scaling the image in GraphicsView and Scene is cutting the right border of image
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?
Code:
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();
Re: Scaling the image in GraphicsView and Scene is cutting the right border of image
Quote:
I’m trying to scale a image to fit in the QGraphicView.
Then you should scale to QGraphicsView's hieght.
Code:
//QPixmap scaledImage = QPixmap(originalImage.scaledToHeight( (int) mScene->height(), Qt::SmoothTransformation) );
QPixmap scaledImage
= originalImage.
scaledToHeight(ui
->graphicsView
->height
(), Qt
::SmoothTransformation);
Re: Scaling the image in GraphicsView and Scene is cutting the right border of image
Thanks, I have tried it now, but not worked
Re: Scaling the image in GraphicsView and Scene is cutting the right border of image
Quote:
Thanks, I have tried it now, but not worked
What is the output like?
Re: Scaling the image in GraphicsView and Scene is cutting the right border of image
Now it is working! I have set the width too small for images with big width. That is why the right border of the image was cut.
So I first calculate the ratio of width and height, and scale on the height or the width, depending of the condition.
Thanks for the help, the scaling on the graphicsview and not on the scene was useful.