Qt 图片显示 动态选择图片显示

在 Qt 中,显示图片通常使用 QLabel 和 QPixmap 进行图像的加载和显示。QPixmap 是专门用于显示图像的类,而 QLabel 则是一个可以容纳图片的小部件。

1、使用 QLabel 和 QPixmap 来显示图片:

bash 复制代码
#include <QApplication>
#include <QLabel>
#include <QPixmap>
#include <QVBoxLayout>
#include <QWidget>

class ImageWindow : public QWidget {
public:
    ImageWindow() {
        QLabel *imageLabel = new QLabel(this); // 创建 QLabel 用于显示图片

        // 加载图片
        QPixmap pixmap(":/images/sample.png"); // 图片路径,可以是绝对路径或资源文件路径

        // 设置 QLabel 的内容为 QPixmap
        imageLabel->setPixmap(pixmap);

        // 自动适应 QLabel 的大小
        imageLabel->setScaledContents(true);
        
        // 设置窗口布局
        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(imageLabel);
        setLayout(layout);
        
        setWindowTitle("图片显示示例");
        resize(400, 400); // 设置窗口大小
    }
};

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

    ImageWindow window;
    window.show();

    return app.exec();
}
  • 说明:
bash 复制代码
QPixmap:

QPixmap 是用来加载和显示图像的类,支持多种图片格式,包括 PNG、JPG、BMP 等。
QPixmap pixmap("path") 加载图片,路径可以是本地文件路径,也可以是 Qt 资源文件系统中的路径(以 :/ 开头)。
QLabel:

QLabel 可以显示文本或图像,通过 setPixmap() 函数可以将 QPixmap 设置为图片内容。
setScaledContents(true) 会让图片根据 QLabel 大小自动缩放。

2、资源文件中的图片加载:

在 Qt 项目中,如果图片是作为资源文件包含进来的(通过 .qrc 文件),可以通过 😕 资源路径来访问。例如:

bash 复制代码
QPixmap pixmap(":/images/sample.png");
例子中的关键部分:
QPixmap:用于加载图片文件。
QLabel:用于显示图片。
setScaledContents(true):图片会根据 QLabel 的大小自动缩放,以适应窗口变化。

3、动态选择图片显示

如果想要让用户选择图片并显示在窗口中,可以结合 QFileDialog 实现。

bash 复制代码
#include <QApplication>
#include <QLabel>
#include <QPixmap>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QPushButton>
#include <QWidget>

class ImageWindow : public QWidget {
    QLabel *imageLabel;

public:
    ImageWindow() {
        imageLabel = new QLabel(this);
        imageLabel->setScaledContents(true);

        QPushButton *button = new QPushButton("选择图片", this);

        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(imageLabel);
        layout->addWidget(button);

        connect(button, &QPushButton::clicked, this, &ImageWindow::selectImage);

        setWindowTitle("动态图片显示");
        resize(400, 400);
    }

private slots:
    void selectImage() {
        QString fileName = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("图片文件 (*.png *.jpg *.bmp)"));

        if (!fileName.isEmpty()) {
            QPixmap pixmap(fileName);
            imageLabel->setPixmap(pixmap);
            imageLabel->adjustSize();  // 调整 QLabel 大小适应图片
        }
    }
};

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

    ImageWindow window;
    window.show();

    return app.exec();
}
  • 动态选择图片并显示的案例说明:
    用户点击按钮时,打开文件选择对话框,让用户选择图片文件。
    使用 QFileDialog::getOpenFileName() 来选择图片文件,选择的文件路径作为 QPixmap 的输入,显示在 QLabel 中。

总结:

QPixmap 和 QLabel 是 Qt 中显示图片的常用组合。

可以通过 setPixmap() 设置图片,通过 setScaledContents() 让图片自适应窗口。

结合 QFileDialog 可以让用户动态选择图片并显示。

相关推荐
南境十里·墨染春水12 分钟前
C++ 笔记 thread
java·开发语言·c++·笔记·学习
南境十里·墨染春水13 分钟前
C++ 笔记 高级线程同步原语与线程池实现
java·开发语言·c++·笔记·学习
来自远方的老作者1 小时前
第10章 面向对象-10.4 继承
开发语言·python·继承·单继承·多继承·super函数
逻辑驱动的ken1 小时前
Java高频面试考点场景题09
java·开发语言·数据库·算法·oracle·哈希算法·散列表
小手cool1 小时前
如何在Java中根据另一个配对集合对一个集合进行排序
java·开发语言
升鲜宝供应链及收银系统源代码服务2 小时前
OMS 订单模块重构正式文档(一)---升鲜宝生鲜配送供应链管理系统
java·开发语言·重构·生鲜配送源代码·生鲜供应链源代码
qq_12084093712 小时前
Three.js 工程向:GLTFLoader 管线、Draco/KTX2 与资源管理
开发语言·javascript·ecmascript
下地种菜小叶2 小时前
定时任务系统怎么设计?一次讲清任务注册、分布式调度、幂等执行与失败补偿
java·开发语言·数据库·oracle·rabbitmq
csbysj20203 小时前
业务代表模式
开发语言
sghuter3 小时前
AI重塑工程师:未来核心能力全景图
开发语言·perl·composer·symfony