PDA

View Full Version : Scaling the image in GraphicsView and Scene is cutting the right border of image



icedecker
14th May 2013, 22:20
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 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();

Santosh Reddy
15th May 2013, 10:04
I’m trying to scale a image to fit in the QGraphicView.
Then you should scale to QGraphicsView's hieght.


//QPixmap scaledImage = QPixmap(originalImage.scaledToHeight( (int) mScene->height(), Qt::SmoothTransformation) );
QPixmap scaledImage = originalImage.scaledToHeight(ui->graphicsView->height(), Qt::SmoothTransformation);

icedecker
15th May 2013, 12:52
Thanks, I have tried it now, but not worked

Santosh Reddy
15th May 2013, 13:48
Thanks, I have tried it now, but not worked
What is the output like?

icedecker
15th May 2013, 13:58
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.