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);