1.让label显示图片并自适应
ui->label->setPixmap(QPixmap(":/1/2.png")); //图片的路径是将图片添加到资源路径中复制来的
ui->label->setScaledContents(true);
2.打开文件对话框选择一张图片放到label上,自适应。(这种方法不用添加资源的)
connect(ui->pushButton,&QPushButton::released,
[=]()
{
QString path=QFileDialog::getOpenFileName(
this,
"open",
"/",
"image(*.png)"
);
if(path!="")
{
QPixmap image(path);
image=image.scaled(ui->label_7->width(),ui->label_7->height(),
Qt::KeepAspectRatio,Qt::SmoothTransformation);
ui->label_7->setPixmap(image);
}
}
);