QVBoxLayout添加图片

QVBoxLayout添加图片

 

#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QImage>
#include <QPixmap>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QWidget window;
    QVBoxLayout *layout = new QVBoxLayout(&window);

    // 创建 QLabel
    QLabel *imageLabel = new QLabel(&window);

    // 加载 QImage
    QImage image("path/to/your/image.jpg"); // 使用你的图片路径
    image.convertToFormat(QImage::Format_RGB32); // 如果需要转换格式

    // 将 QImage 转换为 QPixmap 并设置为 QLabel 的 pixmap
    QPixmap pixmap = QPixmap::fromImage(image);
    imageLabel->setPixmap(pixmap);

    // 将 QLabel 添加到布局中
    layout->addWidget(imageLabel);

    window.show();
    return app.exec();
}

 

 

 

 

#######################

posted @ 2024-02-01 21:33  西北逍遥  阅读(22)  评论(0编辑  收藏  举报