问:我如何调整图像大小并保持其比例? [英] Qt: How do I resize an image and maintain its proportions?

查看:408
本文介绍了问:我如何调整图像大小并保持其比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此Qt 示例显示了如何调整图像大小包含在对话框中,以便在调整对话框大小时,图像会相应地延伸。

This Qt example shows how to resize an image contained in a dialog, so that when the dialog is resized, the image stretches accordingly.

如何以相同的方式调整图像大小,而不会扭曲图像/保持其比例相同?

How can I resize an image in the same way, without distorting it / keeping its proportions the same?

当然如果对话框的宽高比与图像不同,我会得到一个灰色区域。

我找到了 Qt :: KeepAspectRatio 枚举,但不是与其一起使用的功能。

Of course if the width/height ratio of the dialog is different from the one of the image, I will get a "grey" area.
I found the Qt::KeepAspectRatio enum, but not the function to use it with.

更新:这是我正在尝试的代码:

Update: This is the code I am trying with:

QImage image(path);
QImage image2 = image.scaled(200, 200, Qt::KeepAspectRatio);
QLabel *plotImg = new QLabel;
plotImg->setScaledContents(true);
plotImg->setPixmap(QPixmap::fromImage(image2));

调整标签大小时,图像不会保持恒定的宽高比。并且在重新缩放后它会失去解决方案。

The image does not maintain a constant aspect ratio when the label is resized. And it looses resolution after the rescaling.

推荐答案

使用 QImage :: scaled 功能。

QImage img("someimage.png");
QImage img2 = img.scaled(100, 100, Qt::KeepAspectRatio);

如果您需要 QPixmap 存在具有相同名称的功能

这篇关于问:我如何调整图像大小并保持其比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆