QLabel 显示图片大小的自适应

from:
1、https://blog.csdn.net/mangobar/article/details/52300155
2、https://blog.csdn.net/kfy2011/article/details/77867609

设置实现:

qLabel->setScaleContents(true);

上述代码可以在Qt Creator中进行,选择需要设置的QLabel,勾选 scaledContents 属性。

代码实现:

QImage Image;  
Image.load(<span style="font-family:Consolas, 'Courier New', Courier, mono, serif;line-height:18px;background-color:rgb(248,248,248);">"d:/test.jpg"</span>);  

QPixmap pixmap = QPixmap::fromImage(Image);  

int with = ui->labPic->width();  
int height = ui->labPic->height();  

QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  // 饱满填充  

//QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);  // 按比例缩放  

ui->labPic->setPixmap(fitpixmap);

你可能感兴趣的:(qt)