2
votes

I have a QPixmap and I set it to resize according to ratio as the window is resized. When the images is first loaded in, it is good and clear for that ratio, but as I resize the image, it distorts all of it. It manages to go from this: Image before resizing window to this: Image before resizing window

Relevant Code:

void MainWindow::resizeEvent(QResizeEvent *)
{
    QPixmap pix = ui->labelImage->pixmap()->scaled(ui->labelImage->size(),
                                                   Qt::KeepAspectRatio);
    ui->labelImage->setPixmap(pix);
}
1

1 Answers

4
votes

You're reading the current pixmap from the widget, scaling it, and then writing the scaled pixmap to the widget. Consider what happens if the widget becomes very small and is then resized to something much larger -- you'll get a lot of artifacts due to the scaling transformation used.

I think a better approach would be to store the original full size image in a member of your MainWindow class, say...

QPixmap m_original_pixmap;

Then use a scaled version of that in your resizeEvent member...

void MainWindow::resizeEvent(QResizeEvent *)
{
  QPixmap pix = m_original_pixmap.scaled(ui->labelImage->size(), Qt::KeepAspectRatio);
  ui->labelImage->setPixmap(pix);
}

Not sure if that will clear everything up but should go some way to removing some of the artifacts.

As a side note, if you're concerned about image quality you might consider specifying Qt::SmoothTransformation as the pixmap transformation mode in the scaling operation.