日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

一、代碼如下

QImage *img_mainicon;//主圖標顯示在右上角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");

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

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

這個是幫助中函數解釋

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.圖像被縮放到一個盡可能大的矩形內尺寸,保持高寬比。
  • 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.如果給定的大小為空,則此函數返回空圖像。

See also isNull() and Image Transformations.

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

scaledContents : bool

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

此屬性保存標簽是否會縮放其內容以填充所有可用空間。

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

當啟用時,標簽顯示一個像素圖,它將縮放像素圖來填充可用空間。

This property's default is false.

此屬性的默認值為false。

Access functions:

bool

hasScaledContents() const

void

setScaledContents(bool)

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。