The QImageWriter class provides a format independent interface for writing images to files or other devices. 更多...
The QImageWriter class provides a format independent interface for writing images to files or other devices.
QImageWriter supports setting format specific options, such as the gamma level, compression level and quality, prior to storing the image. If you do not need such options, you can use QImage.save () 或 QPixmap.save () 代替。
To store an image, you start by constructing a QImageWriter object. Pass either a file name or a device pointer, and the image format to QImageWriter's constructor. You can then set several options, such as the gamma level (by calling setGamma ()) and quality (by calling setQuality ()). canWrite () returns true if QImageWriter can write the image (i.e., the image format is supported and the device is open for writing). Call write () to write the image to the 设备。
If any error occurs when writing the image, write () will return false. You can then call error () to find the type of error that occurred, or errorString () to get a human readable description of what went wrong.
调用 supportedImageFormats () for a list of formats that QImageWriter can write. QImageWriter supports all built-in image formats, in addition to any image format plugins that support writing.
This enum describes errors that can occur when writing images with QImageWriter .
| 常量 | 值 | 描述 |
|---|---|---|
| QImageWriter.DeviceError | 1 | QImageWriter encountered a device error when writing the image data. Consult your device for more details on what went wrong. |
| QImageWriter.UnsupportedFormatError | 2 | Qt does not support the requested image format. |
| QImageWriter.UnknownError | 0 | An unknown error occurred. If you get this value after calling write (), it is most likely caused by a bug in QImageWriter . |
构造空 QImageWriter object. Before writing, you must call setFormat () to set an image format, then setDevice () or setFileName ().
构造 QImageWriter object using the device device and image format format .
构造 QImageWriter objects that will write to a file with the name fileName , using the image format format 。若 format 不是 provided, QImageWriter will detect the image format by inspecting the extension of fileName .
返回 true 若 QImageWriter can write the image; i.e., the image format is supported and the assigned device is open for reading.
另请参阅 write (), setDevice (),和 setFormat ().
Returns the compression of the image.
另请参阅 setCompression ().
Returns the device currently assigned to QImageWriter , or 0 if no device has been assigned.
另请参阅 setDevice ().
返回最后发生的错误类型。
另请参阅 ImageWriterError and errorString ().
Returns a human readable description of the last error that occurred.
另请参阅 error ().
若目前赋值设备是 QFile ,或者若 setFileName () has been called, this function returns the name of the file QImageWriter writes to. Otherwise (i.e., if no device has been assigned or the device is not a QFile ), an empty QString 被返回。
另请参阅 setFileName () 和 setDevice ().
返回格式 QImageWriter uses for writing images.
另请参阅 setFormat ().
Returns the gamma level of the image.
另请参阅 setGamma ().
Returns the quality level of the image.
另请参阅 setQuality ().
This is an image format specific function that set the compression of an image. For image formats that do not support setting the compression, this value is ignored.
The value range of 压缩 depends on the image format. For example, the "tiff" format supports two values, 0(no compression) and 1(LZW-compression).
另请参阅 压缩 ().
集 QImageWriter 's device to device . If a device has already been set, the old device is removed from QImageWriter and is otherwise left unchanged.
若设备尚未打开, QImageWriter will attempt to open the device in QIODevice.WriteOnly mode by calling open(). Note that this does not work for certain devices, such as QProcess , QTcpSocket and QUdpSocket , where more logic is required to open the device.
另请参阅 device () and setFileName ().
Sets the file name of QImageWriter to fileName . 内部, QImageWriter will create a QFile and open it in QIODevice.WriteOnly mode, and use this file when writing images.
另请参阅 fileName () 和 setDevice ().
设置格式 QImageWriter will use when writing images, to format . format 是 case insensitive text string. Example:
QImageWriter writer; writer.setFormat("png"); // same as writer.setFormat("PNG");
可以调用 supportedImageFormats () for the full list of formats QImageWriter 支持。
另请参阅 format ().
This is an image format specific function that sets the gamma level of the image to gamma . For image formats that do not support setting the gamma level, this value is ignored.
The value range of gamma depends on the image format. For example, the "png" format supports a gamma range from 0.0 to 1.0.
This is an image format specific function that sets the quality level of the image to quality . For image formats that do not support setting the quality, this value is ignored.
The value range of quality depends on the image format. For example, the "jpeg" format supports a quality range from 0 (low quality, high compression) to 100 (high quality, low compression).
另请参阅 quality ().
Sets the image text associated with the key key to text . This is useful for storing copyright information or other information about the image. Example:
QImage image("some/image.jpeg"); QImageWriter writer("images/outimage.png", "png"); writer.setText("Author", "John Smith"); writer.write(image);
If you want to store a single block of data (e.g., a comment), you can pass an empty key, or use a generic key like "Description".
The key and text will be embedded into the image data after calling write ().
支持此选项的实现是透过 QImageIOHandler.Description .
该函数在 Qt 4.1 引入。
另请参阅 QImage.setText () 和 QImageReader.text ().
Returns the list of image formats supported by QImageWriter .
By default, Qt can write the following formats:
| Format | 描述 |
|---|---|
| BMP | Windows 位图 |
| JPG | Joint Photographic Experts Group (联合摄影专家组) |
| JPEG | Joint Photographic Experts Group (联合摄影专家组) |
| PNG | Portable Network Graphics (便携式网络图形) |
| PPM | Portable Pixmap (便携式像素图) |
| TIFF | 标签化图像文件格式 |
| XBM | X11 Bitmap (X11 位图) |
| XPM | X11 Pixmap (X11 像素图) |
Reading and writing SVG files is supported through Qt's SVG Module .
注意: QApplication 实例必须被创建在调用此函数之前。
另请参阅 setFormat (), QImageReader.supportedImageFormats (), and QImageIOPlugin .
Returns true if the writer supports option ;否则 returns false.
Different image formats support different options. Call this function to determine whether a certain option is supported by the current format. For example, the PNG format allows you to embed text into the image's metadata (see text()).
QImageWriter writer(fileName); if (writer.supportsOption(QImageIOHandler.Description)) writer.setText("Author", "John Smith");
Options can be tested after the writer has been associated with a format.
该函数在 Qt 4.2 引入。
另请参阅 QImageReader.supportsOption () and setFormat ().
写入图像 image to the assigned device or file name. Returns true on success; otherwise returns false. If the operation fails, you can call error () to find the type of error that occurred, or errorString () to get a human readable description of the error.
另请参阅 canWrite (), error (),和 errorString ().