一、需求
实现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);