可以使用 Qt 的 QLabel 控件来显示图像,具体步骤如下:

  1. 加载图像文件,例如使用 QImage 类 的 load() 方法加载图像文件。

  2. 创建一个 QLabel 控件,并将其添加到 QWidget 中。

  3. 将加载的图像设置为 QLabel 控件的背景图像,使用 QPalette 类 的 setBrush() 方法设置 QLabel 控件的背景图片。

示例代码如下:

// 加载图片
QImage image;
image.load('image.jpg');

// 创建 QLabel 控件
QLabel* label = new QLabel(this);
label->setGeometry(0, 0, image.width(), image.height());

// 设置 QLabel 控件的背景图片
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(image));
label->setPalette(palette);
label->setAutoFillBackground(true);

注意:上述代码中,this 指代当前 QWidget 控件的指针。如果要在其他 QWidget 控件中显示图像,则需要修改 this 的值。


原文地址: https://www.cveoy.top/t/topic/nlHd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录