1

I have this code:

void MainWindow::on_pushButton_clicked() //displaying an image by clicking the button
{
    if (ui->graphicsView->scene() != NULL)
        ui->graphicsView->scene()->clear();
    pm.load("E:/QtProjects/resize/1.png");
    item = new QGraphicsPixmapItem(pm.scaled(ui->graphicsView->size(), Qt::KeepAspectRatio));
    graphics->addItem(item);
    ui->graphicsView->setScene(graphics);
}

void MainWindow::resizeEvent(QResizeEvent *event) //resize the image if the window was resized
{
    if (ui->graphicsView->scene() != NULL)
        ui->graphicsView->fitInView(ui->graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
}

When I load an image and resize the window it works fine, but when I load the image again it gets smaller if the window got smaller or gets bigger when the window got bigger: enter image description here

Resized the window:

Resized the window

Loaded image again:

enter image description here

How to fix this?

Ogurchik
  • 53
  • 5
  • Does this answer your question? [How to resize QGraphicsView properly?](https://stackoverflow.com/questions/50200972/how-to-resize-qgraphicsview-properly) – Abderrahmene Rayene Mihoub Aug 16 '23 at 12:45
  • [Qt QGraphicsview how to hook to resize event](https://stackoverflow.com/questions/24738458/qt-qgraphicsview-how-to-hook-to-resize-event) | [Qt5 C++ QGraphicsView: Images don't fit view frame](https://stackoverflow.com/questions/17028680/qt5-c-qgraphicsview-images-dont-fit-view-frame) | [QGraphicsView::fitInView](https://doc.qt.io/qt-6/qgraphicsview.html#fitInView) – Abderrahmene Rayene Mihoub Aug 16 '23 at 12:51

1 Answers1

2

The issue you're facing is because you're using fitInView in the resizeEvent without taking into consideration the original size of the loaded image. This causes the newly loaded image to be scaled based on the current size of the QGraphicsView rather than its original size.

To fix this, you need to modify your code to ensure that the newly loaded image is scaled correctly based on its original size, regardless of the current size of the QGraphicsView.

add ui->graphicsView->fitInView(item, Qt::KeepAspectRatio); to your code.

void MainWindow::resizeEvent(QResizeEvent *event)
{
    QMainWindow::resizeEvent(event);

    if (item)
    {
        ui->graphicsView->fitInView(item, Qt::KeepAspectRatio);
    }
}

in mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    QMainWindow::resizeEvent(event);

    if (item)
    {
        ui->graphicsView->fitInView(item, Qt::KeepAspectRatio);
    }
}

void MainWindow::on_pushButton_clicked()
{
    scene->clear();
    pm.load("/home/parisa/Pictures/Screenshots/cat.png");  // Replace with your image path
    item = new QGraphicsPixmapItem(pm);
    scene->addItem(item);
    ui->graphicsView->fitInView(item, Qt::KeepAspectRatio);
}

MainWindow::~MainWindow()
{
    delete ui;
}

enter image description here

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38