QT学习之QImage::scaled

QT中图片的比例变换,为了适应控件的大小,采用QImage、QPixmap等绘图设备类提供的scaled()函数,下面是Qt文档对于scaled()函数介绍:

函数原型:

QImage QImage::scaled ( int width, int height,  Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio,  Qt::TransformationMode transformMode = Qt::FastTransformation ) const  

This is an overloaded function.

Returns a copy of the image scaled to a rectangle with the given width and height according to the given AspectRatioMode and TransformMode.

If either the width or the height is zero or negative, this function returns a null image.

这是一个重载函数,按照指定的宽和高,根据纵横比(Aspect Ratio)模式和转换模式从原有图像返回一个经过比例转换的图像,如果宽高为0,返回一个空图像

所以,获取控件的改变后的宽高,就能设定图像转换的宽高转换比例,用scaled()的返回重新进行绘图即可自适应窗口。

其中:

纵横比:一个物体的水平宽度除以垂直高度所得比例值,或一个物体的垂直高度除以水平宽度所得的比例值。纵横比,即一个图像的宽度除以它的高度所得的比例,通常表示为 "x:y" or "x×y",其中的冒号和叉号表示中文的“比”之意。目前,在电影工业中最常被使用的是 anamorphic 比例(即 2.39:1)[1]。传统的 4:3(1.33:1)仍然被使用于现今的许多电视画面上,而它成功的后继规格 16:9(1.78:1)则被用于高清晰度电视和欧洲的数位电视上。QImage中有三种选择,如图所示。

转换模式:

例如:


 QImage _image;
      _image.load(tab1_image);
      ui->tab->setAutoFillBackground(true);   // 这个属性一定要设置
      QPalette pal;
      pal.setBrush(QPalette::Background, QBrush(_image.scaled(ui->tab->size(), Qt::IgnoreAspectRatio)));
      ui->tab->setPalette(pal);
 
原文地址:https://www.cnblogs.com/qixianyu/p/6891054.html