一、需求
實現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);