天天看點

linux qt界面 視窗大小,Qt實作圖像自适應視窗大小之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.

翻譯:

這是一個重載函數,按照指定的寬和高,根據縱橫比模式和轉換模式從原有圖像傳回一個經過比例轉換的圖像,如果寬高為0,傳回一個空圖像

是以,擷取控件的改變後的寬高,就能設定圖像轉換的寬高轉換比例,用scaled()的傳回重新進行繪圖即可自适應視窗,以下是個例子:

voidWidget::paintEvent(QPaintEvent *)

{

QImage img((unsignedchar*)im.data,im.cols,

im.rows,QImage::Format_RGB888);

QPainter painter(this);

if(0==flag)

painter.drawImage(0,0,nImg);

elseif(1==flag)

{

nImg=img.scaled(width(),height());

painter.drawImage(0,0,nImg);

}

}

Ps:

圖像是按比例變化的,如果放大很多,會出現模糊等現象。

linux qt界面 視窗大小,Qt實作圖像自适應視窗大小之scaled()函數使用