QPixmap::scaled
是Qt中用于圖像縮放的核??法,其作?和?法如下:
?一、核心作用?
- ?圖像尺寸調整?
根據指定尺寸對圖像進?等?例或?等?例縮放,?持放?和縮?操作。 - ?保持寬高比?
通過AspectRatioMode
參數控制是否保持原始圖像的寬??。 - ?渲染質量優化?
提供快速變換(FastTransformation
)和平滑變換(SmoothTransformation
)兩種模式,平衡性能與畫質。
?二、參數詳解?
1. ?基本參數?
QPixmap scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio,Qt::TransformationMode transformMode = Qt::FastTransformation) const
- ?
size
?:目標尺寸(QSize
或width, height
)。 - ?
aspectRatioMode
?:寬??控制模式,可選:Qt::IgnoreAspectRatio
:忽略寬??,強制拉伸?目標尺寸。Qt::KeepAspectRatio
:保持寬??,縮放到最?內接矩形。Qt::KeepAspectRatioByExpanding
:保持寬??,縮放到最?外接矩形。
- ?
transformMode
?:渲染質量模式:Qt::FastTransformation
:快速但可能有鋸齒。Qt::SmoothTransformation
:平滑但性能開銷較?。
2. ?重載版本?
QPixmap scaled(int width, int height, ...) // 直接指定寬高
?三、典型用法示例?
1. ?等比例縮放(常用)?
QPixmap pixmap("image.png");
QPixmap scaled = pixmap.scaled(400, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- 保持原始寬??,縮放到不超過400×300的最?尺寸。
2. ?強制拉伸填充?
pixmap.scaled(200, 200, Qt::IgnoreAspectRatio); // 可能變形
3. ?高質量放大?
pixmap.scaled(800, 600, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
?四、應用場景?
- ?控件適配?
在QLabel
中顯示縮放后的圖像時,通常結合setScaledContents(true)
使圖像?適應控件??。 - ?性能優化?
?圖縮?時優先使?FastTransformation
,?圖放?時推薦SmoothTransformation
。 - ?動態交互?
在?標滾輪縮放等交互場景中動態調?scaled
。
?五、注意事項?
- ?內存管理?:縮放會?成新圖像,需避免頻繁調?。
- ?SVG與PNG差異?:SVG?量圖縮放?失真,PNG位圖可能失真。
- ?DPI適配?:?分辨率屏幕需結合
devicePixelRatio
調整。