QImageIOPlugin Class

QImageIOPlugin 类定义了编写图像格式插件的接口。更多

头文件: #include <QImageIOPlugin>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承: QObject

注意:该类中的所有函数都是可重入的

公共类型

flags Capabilities
enum Capability { CanRead, CanWrite, CanReadIncremental }

公共函数

QImageIOPlugin(QObject *parent = nullptr)
virtual ~QImageIOPlugin()
virtual QImageIOPlugin::Capabilities capabilities(QIODevice *device, const QByteArray &format) const = 0
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const = 0

详细说明

QImageIOPlugin 是一个用于创建QImageIOHandler 对象的工厂,这些对象被QImageReaderQImageWriter 内部使用,以便为 Qt 添加对不同图像格式的支持。

编写图像 I/O 插件可通过子类化该基类、重新实现纯虚函数capabilities() 和create() 以及使用Q_PLUGIN_METADATA() 宏导出该类来实现。详情请参阅如何创建 Qt 插件

图像格式插件可支持三种功能:读取(CanRead )、写入(CanWrite )和增量读取(CanReadIncremental )。在您的子类中重新实现capabilities() 以公开图像格式的功能。

create() 应创建一个QImageIOHandler 子类的实例,并正确设置所提供的设备和格式,然后返回此处理程序。

插件的 json 元数据文件需要包含插件支持的图像格式信息,以及相应的 MIME 类型(每种格式一种)。例如,对于 jpeg 插件,该文件可以如下所示:

{
  "Keys": [ "jpg", "jpeg" ],
  "MimeTypes": [ "image/jpeg", "image/jpeg" ]
}

不同的插件可以支持不同的功能。例如,可能有一个插件支持读取 GIF 格式,另一个支持写入。Qt XML 将根据capabilities() 的返回值,为工作选择正确的插件。如果多个插件支持相同的功能,Qt 将任意选择一个。

另请参阅 QImageIOHandler如何创建 Qt 插件

成员类型文档

枚举 QImageIOPlugin::Capability
flags QImageIOPlugin::Capabilities

该枚举描述了QImageIOPlugin 的能力。

常量描述
QImageIOPlugin::CanRead0x1插件可以读取图像。
QImageIOPlugin::CanWrite0x2插件可以写入图像。
QImageIOPlugin::CanReadIncremental0x4插件可以增量方式读取图像。

Capabilities 类型是QFlags<Capability> 的类型定义。它存储能力值的 OR 组合。

成员函数文档

[explicit] QImageIOPlugin::QImageIOPlugin(QObject *parent = nullptr)

使用给定的parent 构建图像插件。导出插件的 moc 生成代码会自动调用该插件。

[virtual noexcept] QImageIOPlugin::~QImageIOPlugin()

销毁图片格式插件。

您不必明确地调用它。当插件不再使用时,Qt 会自动销毁。

[pure virtual] QImageIOPlugin::Capabilities QImageIOPlugin::capabilities(QIODevice *device, const QByteArray &format) const

根据device 中的数据和格式format ,返回插件的能力。如果device0 ,则只需报告能否读取或写入该格式。否则,它应尝试确定给定格式(或插件支持的任何格式,如果format 为空)是否可从device 读取或写入。它应在不改变device 状态的情况下完成此操作(通常使用QIODevice::peek() )。

例如,如果QImageIOPlugin 支持 BMP 格式,format 为空或"bmp" ,且设备中的数据以字符"BM" 开头,则此函数应返回CanRead 。如果format"bmp"device0 ,且处理程序支持读写,则此函数应返回CanRead |CanWrite

格式名称一律小写。

[pure virtual] QImageIOHandler *QImageIOPlugin::create(QIODevice *device, const QByteArray &format = QByteArray()) const

创建并返回QImageIOHandler 子类,并设置deviceformatformat 必须来自插件元数据"Keys" 条目中所列的值,或者为空。如果为空,则device 中的数据必须已被capabilities() 方法识别(格式同样为空)。

格式名称始终以小写字母表示。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.