这个Qt示例展示了如何在对话框中调整图片大小,当对话框被调整大小时,图像会相应地拉伸。
我如何以同样的方式调整图像的大小,同时保持其比例而不失真?
当然,如果对话框的宽度/高度比与图像的比例不同,我将得到一个“灰色”区域。
我找到了Qt::KeepAspectRatio
枚举,但不知道要使用哪个函数。
更新:这是我正在尝试的代码:
QImage image(path);
QImage image2 = image.scaled(200, 200, Qt::KeepAspectRatio);
QLabel *plotImg = new QLabel;
plotImg->setScaledContents(true);
plotImg->setPixmap(QPixmap::fromImage(image2));
当标签被调整大小时,图像未能保持恒定的纵横比,并且在重新缩放后失去了分辨率。
scaledContents
属性后,QLabel
才会调整所显示的像素图的大小。此外,您可能希望将Qt::SmoothTransformation
作为scaled
函数的最后一个参数传递。 - Guilherme BernalscaledContents
。您可以通过响应窗口的resizeEvent
事件用一个新的缩放图像重置pixmap。 - Guilherme Bernal