读写图像文件

读取图像的最常用方法是通过QImageQPixmap 的构造函数,或调用QImage::load() 和QPixmap::load() 函数。此外,Qt XML 还提供了QImageReader 类,可对该过程进行更多控制。根据图像格式的底层支持,该类提供的函数可以节省内存并加快图像加载速度。

同样,Qt XML 提供的QImageWriter 类支持在存储图像前设置特定格式选项,如伽玛级别、压缩级别和质量。如果不需要这些选项,可以使用QImage::save() 或QPixmap::save() 代替。

QMovie

QMovie 是一个用于显示动画的方便类,内部使用 类。创建后, 类将提供运行和控制给定动画的各种功能。QImageReader QMovie

QImageReaderQImageWriter 类依赖于QImageIOHandler 类,该类是 Qt 中所有图像格式的通用图像 I/O 接口。QImageIOHandler 对象在QImageReaderQImageWriter 内部使用,以便为 Qt 添加对不同图像格式的支持。

支持的文件格式列表可通过QImageReader::supportedImageFormats() 和QImageWriter::supportedImageFormats() 函数获得。Qt 默认支持几种文件格式,此外还可以通过插件添加新格式。QImageReaderQImageWriter 类文档中列出了当前支持的格式。

Qt 的插件机制也可用于编写自定义图像格式处理程序。具体方法是派生自QImageIOHandler 类,并创建一个QImageIOPlugin 对象,该对象是创建QImageIOHandler 对象的工厂。安装插件后,QImageReaderQImageWriter 将自动加载插件并开始使用。

© 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.