改变窗口组件的情况下改变窗口背景图片的大小,QImage、QPixmap等绘图设备类都提供scaled()函数。
scaled函数:
scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation) const
第一个参数代表缩放后的尺寸
第二个参数有两种表现形式,分别为Qt::IgnoreAspectRatio和Qt::KeepAspectRatio
- KeepAspectRatio:保持纵横比(常用)
- IgnoreAspectRatio:忽略纵横比,图片会撑满整个界面
第三个参数也有两种表现形式,分别为Qt::FastTransformation和Qt::SmoothTransformation
- FastTransformation模式表示快速变换 (快速获得图片,但质量差些)
- SmoothTransformation模式表示平滑变换 (速度慢些,但质量好些)
以下代码例子可以实现图像的缩放为宽400,高300像素的图片
QPixmap pixmap("path");
pixmap = pixmap.scaled(400, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation);