Qt label加载图片,父窗口便利子窗口控件

int size = 12;
for (int i = 1; i <= size; i++)
{
QString filename = QString("./image/%2%3").arg(i).arg(".jpg");
//qDebug() << "filename:" << filename << endl;
QImage* img = new QImage, *scaledimg = new QImage;//分别保存原图和缩放之后的图片
//加载图像,如加载失败,则弹出对话框,提示打开图像失败
if (!(img->load(filename)))
{
QMessageBox::information(this, QStringLiteral("打开图像失败"), QStringLiteral("打开图像失败!"));
delete img;
return;
}

//获取图像的宽度和高度
int ImageWidth = img->width(), ImageHeight = img->height();
//定义缩放的图片的宽度和高度
int SmallWidth, SmallHeight;
//设置label的位置和宽高

int Mul; //记录图片与label大小的比例,用于缩放图片
if (ImageWidth / 200 >= ImageHeight / 150)
{
Mul = ImageWidth / 200;
}
else
{
Mul = ImageHeight / 150;
}
SmallWidth = ImageWidth / Mul;
SmallHeight = ImageHeight / Mul;
*scaledimg = img->scaled(SmallWidth, SmallHeight, Qt::KeepAspectRatio);

QString lablename = "DeviceImage" + QString::number(i, 10);
QLabel *label = ui.DeviceMonitorgroupBox->findChild<QLabel *>(lablename);
if (label)
{
label->setPixmap(QPixmap::fromImage(*scaledimg));
}
else
return;

QString devicename = "DeviceName" + QString::number(i, 10);
QLabel *name = ui.DeviceMonitorgroupBox->findChild<QLabel *>(devicename);
if (name)
{
name->setText(QStringLiteral("设备在线"));
name->setAlignment(Qt::AlignHCenter);
}
else
return;

}

原文地址:https://www.cnblogs.com/LuckCoder/p/10862809.html