欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Qt 实现QT控件中的QLabel显示图片并自适应显示

程序员文章站 2024-01-19 12:41:22
...

一、需求

实现QT控件中的QLabel显示图片,并自适应显示。

二、代码

QImage Image;
Image.load(":/image/image/logo.jpg");
QPixmap pixmap = QPixmap::fromImage(Image);
int with = ui->label_logo->width();
int height = ui->label_logo->height();
QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 饱满填充
//QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放
ui->label_logo->setPixmap(fitpixmap);

 

相关标签: QtCreator