简体   繁体   English

Qt | QPixmap缩放比例错误

[英]Qt | QPixmap scaling incorrectly

I have a QPixmap and I set it to resize according to ratio as the window is resized. 我有一个QPixmap,我将其设置为在调整窗口大小时根据比例进行调整。 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: 它设法做到这一点: 调整窗口大小之前的图像 to this: 对此: 调整窗口大小之前的图像

Relevant Code: 相关代码:

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

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... 我认为更好的方法是将原始的全尺寸图像存储在MainWindow类的成员中,例如...

QPixmap m_original_pixmap;

Then use a scaled version of that in your resizeEvent member... 然后在您的resizeEvent成员中使用该版本的缩放版本...

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. 附带说明一下,如果您担心图像质量,可以考虑在缩放操作中将Qt :: SmoothTransformation指定为像素图转换模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM