QT中在label中插入图片自适应label大小等比缩放
一、代碼如下
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt 的一些心得
- 下一篇: 利用QT实现X轴为时间动态显示曲线