读取和写入图像文件
读取图像最常见的方式是通过QImage 和QPixmap 的构造函数,或者调用QImage::load()和QPixmap::load()函数。此外,Qt还提供了QImageReader 类,该类可对读取过程进行更精细的控制。根据图像格式的底层支持情况,该类提供的函数可以节省内存并加快图像加载速度。
同样,Qt 提供了QImageWriter 类,该类支持在存储图像之前设置特定于格式的选项,例如伽马值、压缩级别和质量。如果您不需要这些选项,可以使用QImage::save() 或QPixmap::save() 代替。
| QMovie QMovie 是一个用于显示动画的便捷类,内部使用QImageReader 类。一旦创建,QMovie 类便会提供各种函数,用于运行和控制给定的动画。 |
|
QImageReader 和QImageWriter 类依赖于QImageIOHandler 类,该类是 Qt 中所有图像格式的通用图像 I/O 接口。QImageReader 和QImageWriter 内部使用QImageIOHandler 对象,以向 Qt 添加对不同图像格式的支持。
可通过QImageReader::supportedImageFormats() 和QImageWriter::supportedImageFormats() 函数获取支持的文件格式列表。Qt 默认支持多种文件格式,此外还可以通过插件添加新格式。当前支持的格式在QImageReader 和QImageWriter 类的文档中列出。
Qt 的插件机制也可用于编写自定义图像格式处理程序。具体实现方式是继承QImageIOHandler 类,并创建一个QImageIOPlugin 对象,该对象是用于生成QImageIOHandler 对象的工厂。当插件安装后,QImageReader 和QImageWriter 将自动加载该插件并开始使用它。
© 2026 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.
