日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT中在label中插入图片自适应label大小等比缩放

發(fā)布時間:2025/4/5 c/c++ 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT中在label中插入图片自适应label大小等比缩放 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、代碼如下

QImage *img_mainicon;//主圖標(biāo)顯示在右上角lable中img_mainicon =new QImage;//新建一個image對象img_mainicon->load(":/image/images/haiyan.jpg"); //載入圖片到img對象中img_mainicon->scaled(ui->label_mainicon->size(),Qt::KeepAspectRatio);//把圖片ui->label_mainicon->setScaledContents(true);ui->label_mainicon->setPixmap(QPixmap::fromImage(*img_mainicon)); //將圖片放入label,使用setPixmap,注意指針*img//ui->label_mainicon->setAlignment(Qt::AlignCenter); //將圖片放在label中心,用縮放了就不需要了

二、詳解

其中第一,第二,第三行代碼可以整合成一行

QPixmap *pixmap = new QPixmap(":/images/welcome_tlauto.png");

其中路徑可以由資源文件得到

第四條語句:設(shè)置圖片縮放到label尺寸

這個是幫助中函數(shù)解釋

QImage QImage::scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation) const

Returns a copy of the image scaled to a rectangle defined by the given size according to the given aspectRatioMode and transformMode.

返回按給定aspectRatioMode和transformMode將圖像縮放到由給定大小定義的矩形的副本

  • If aspectRatioMode is Qt::IgnoreAspectRatio, the image is scaled to size. 圖像按比例縮小。
  • If aspectRatioMode is Qt::KeepAspectRatio, the image is scaled to a rectangle as large as possible inside size, preserving the aspect ratio.圖像被縮放到一個盡可能大的矩形內(nèi)尺寸,保持高寬比。
  • If aspectRatioMode is Qt::KeepAspectRatioByExpanding, the image is scaled to a rectangle as small as possible outside size, preserving the aspect ratio.圖像被縮放到一個盡可能小的矩形尺寸,保持高寬比。

If the given size is empty, this function returns a null image.如果給定的大小為空,則此函數(shù)返回空圖像。

See also isNull() and Image Transformations.

第五條語句:它的作用是設(shè)置label的屬性scaledContents,這個屬性的作用是允許(禁止)label縮放它的內(nèi)容充滿整個可用的空間。特別說明的一點是當(dāng)使能該屬性并且label顯示pixmap時,它能夠縮放pixmap充滿整個可用的空間。

scaledContents : bool

This property holds whether the label will scale its contents to fill all available space.

此屬性保存標(biāo)簽是否會縮放其內(nèi)容以填充所有可用空間。

When enabled and the label shows a pixmap, it will scale the pixmap to fill the available space.

當(dāng)啟用時,標(biāo)簽顯示一個像素圖,它將縮放像素圖來填充可用空間。

This property's default is false.

此屬性的默認(rèn)值為false。

Access functions:

bool

hasScaledContents() const

void

setScaledContents(bool)

總結(jié)

以上是生活随笔為你收集整理的QT中在label中插入图片自适应label大小等比缩放的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。