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:
Resized the window:
Loaded image again:
How to fix this?