QImageWriter Class▲
-
Header: QImageWriter
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
-
qmake: QT += gui
-
Group: QImageWriter is part of https://, io
Detailed Description▲
https:// supports setting format specific options, such as compression level and quality, prior to storing the image. If you do not need such options, you can use https://() or https://() instead.
To store an image, you start by constructing a https:// object. Pass either a file name or a device pointer, and the image format to https://'s constructor. You can then set several options, such as quality (by calling https://()). https://() returns true if https:// can write the image (i.e., the image format is supported and the device is open for writing). Call https://() to write the image to the device.
If any error occurs when writing the image, https://() will return false. You can then call https://() to find the type of error that occurred, or https://() to get a human readable description of what went wrong.
Call https://() for a list of formats that https:// can write. https:// supports all built-in image formats, in addition to any image format plugins that support writing.
https:// assumes exclusive control over the file or device that is assigned. Any attempts to modify the assigned file or device during the lifetime of the https:// object will yield undefined results. If immediate access to a resource is desired, the use of a scope is the recommended method.
For example:
QString imagePath(QStringLiteral("path/image.jpeg"));
QImage image(64, 64, QImage::Format_RGB32);
image.fill(Qt::red);
{
QImageWriter writer(imagePath);
writer.write(image);
}
QFile::rename(imagePath,
QStringLiteral("path/other_image.jpeg"));See Also▲
See also https://, https://, https://, https://
Member Type Documentation▲
enum QImageWriter::ImageWriterError▲
This enum describes errors that can occur when writing images with https://.
|
Constant |
Value |
Description |
|---|---|---|
|
https://::DeviceError |
1 |
https:// encountered a device error when writing the image data. Consult your device for more details on what went wrong. |
|
https://::UnsupportedFormatError |
2 |
Qt does not support the requested image format. |
|
https://::InvalidImageError |
3 |
An attempt was made to write an invalid https://. An example of an invalid image would be a null https://. |
|
https://::UnknownError |
0 |
An unknown error occurred. If you get this value after calling https://(), it is most likely caused by a bug in https://. |
Member Function Documentation▲
QImageWriter::QImageWriter()▲
Constructs an empty https:// object. Before writing, you must call https://() to set an image format, then https://() or https://().
[explicit] QImageWriter::QImageWriter(QIODevice *device, const QByteArray &format)▲
Constructs a https:// object using the device device and image format format.
[explicit] QImageWriter::QImageWriter(const QString &fileName, const QByteArray &format = QByteArray())▲
Constructs a https:// objects that will write to a file with the name fileName, using the image format format. If format is not provided, https:// will detect the image format by inspecting the extension of fileName.
QImageWriter::~QImageWriter()▲
Destructs the https:// object.
bool QImageWriter::canWrite() const▲
Returns true if https:// can write the image; i.e., the image format is supported and the assigned device is open for reading.
See Also▲
See also https://(), https://(), https://()
int QImageWriter::compression() const▲
QIODevice *QImageWriter::device() const▲
Returns the device currently assigned to https://, or nullptr if no device has been assigned.
See Also▲
See also https://()
QImageWriter::ImageWriterError QImageWriter::error() const▲
QString QImageWriter::errorString() const▲
QString QImageWriter::fileName() const▲
If the currently assigned device is a https://, or if https://() has been called, this function returns the name of the file https:// writes to. Otherwise (i.e., if no device has been assigned or the device is not a https://), an empty https:// is returned.
See Also▲
See also https://(), https://()
QByteArray QImageWriter::format() const▲
[static, since 5.12] QList<QByteArray> QImageWriter::imageFormatsForMimeType(const QByteArray &mimeType)▲
Returns the list of image formats corresponding to mimeType.
Note that the https:// instance must be created before this function is called.
This function was introduced in Qt 5.12.
See Also▲
See also https://(), https://()
[since 5.5] bool QImageWriter::optimizedWrite() const▲
Returns whether optimization has been turned on for writing the image.
This function was introduced in Qt 5.5.
See Also▲
See also https://()
[since 5.5] bool QImageWriter::progressiveScanWrite() const▲
Returns whether the image should be written as a progressive image.
This function was introduced in Qt 5.5.
See Also▲
See also https://()
int QImageWriter::quality() const▲
void QImageWriter::setCompression(int compression)▲
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 compression depends on the image format. For example, the "tiff" format supports two values, 0(no compression) and 1(LZW-compression).
See Also▲
See also https://()
void QImageWriter::setDevice(QIODevice *device)▲
Sets https://'s device to device. If a device has already been set, the old device is removed from https:// and is otherwise left unchanged.
If the device is not already open, https:// will attempt to open the device in https:// mode by calling open(). Note that this does not work for certain devices, such as https://, https:// and https://, where more logic is required to open the device.
See Also▲
See also https://(), https://()
void QImageWriter::setFileName(const QString &fileName)▲
Sets the file name of https:// to fileName. Internally, https:// will create a https:// and open it in https:// mode, and use this file when writing images.
See Also▲
See also https://(), https://()
void QImageWriter::setFormat(const QByteArray &format)▲
Sets the format https:// will use when writing images, to format. format is a case insensitive text string. Example:
QImageWriter writer;
writer.setFormat("png"); // same as writer.setFormat("PNG");You can call https://() for the full list of formats https:// supports.
See Also▲
See also https://()
[since 5.5] void QImageWriter::setOptimizedWrite(bool optimize)▲
This is an image format-specific function which sets the optimize flags when writing images. For image formats that do not support setting an optimize flag, this value is ignored.
The default is false.
This function was introduced in Qt 5.5.
See Also▲
See also https://()
[since 5.5] void QImageWriter::setProgressiveScanWrite(bool progressive)▲
This is an image format-specific function which turns on progressive scanning when writing images. For image formats that do not support setting a progressive scan flag, this value is ignored.
The default is false.
This function was introduced in Qt 5.5.
See Also▲
See also https://()
void QImageWriter::setQuality(int quality)▲
Sets the quality setting of the image format to quality.
Some image formats, in particular lossy ones, entail a tradeoff between a) visual quality of the resulting image, and b) encoding execution time and compression level. This function sets the level of that tradeoff for image formats that support it. For other formats, 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 visual quality, high compression) to 100 (high visual quality, low compression).
See Also▲
See also https://()
[since 5.4] void QImageWriter::setSubType(const QByteArray &type)▲
This is an image format specific function that sets the subtype of the image to type. Subtype can be used by a handler to determine which format it should use while saving the image.
For example, saving an image in DDS format with A8R8G8R8 subtype:
QImageWriter writer("some/image.dds");
if (writer.supportsOption(QImageIOHandler::SubType))
writer.setSubType("A8R8G8B8");
writer.write(image);This function was introduced in Qt 5.4.
See Also▲
See also https://()
void QImageWriter::setText(const QString &key, const QString &text)▲
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 https://().
Support for this option is implemented through https://.
See Also▲
See also https://(), https://()
[since 5.5] void QImageWriter::setTransformation(QImageIOHandler::Transformations transform)▲
Sets the image transformations metadata including orientation to transform.
If transformation metadata is not supported by the image format, the transform is applied before writing.
This function was introduced in Qt 5.5.
See Also▲
See also https://(), https://()
[since 5.4] QByteArray QImageWriter::subType() const▲
Returns the subtype of the image.
This function was introduced in Qt 5.4.
See Also▲
See also https://()
[static] QList<QByteArray> QImageWriter::supportedImageFormats()▲
Returns the list of image formats supported by https://.
By default, Qt can write the following formats:
|
Format |
MIME type |
Description |
|---|---|---|
|
BMP |
image/bmp |
Windows Bitmap |
|
JPG |
image/jpeg |
Joint Photographic Experts Group |
|
PNG |
image/png |
Portable Network Graphics |
|
PBM |
image/x-portable-bitmap |
Portable Bitmap |
|
PGM |
image/x-portable-graymap |
Portable Graymap |
|
PPM |
image/x-portable-pixmap |
Portable Pixmap |
|
XBM |
image/x-xbitmap |
X11 Bitmap |
|
XPM |
image/x-xpixmap |
X11 Pixmap |
Reading and writing SVG files is supported through the https:// module. The Qt Image Formats module provides support for additional image formats.
Note that the https:// instance must be created before this function is called.
See Also▲
See also https://(), https://(), https://
[static] QList<QByteArray> QImageWriter::supportedMimeTypes()▲
Returns the list of MIME types supported by https://.
Note that the https:// instance must be created before this function is called.
See Also▲
See also https://(), https://()
[since 5.4] QList<QByteArray> QImageWriter::supportedSubTypes() const▲
Returns the list of subtypes supported by an image.
This function was introduced in Qt 5.4.
bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const▲
Returns true if the writer supports option; otherwise 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.
See Also▲
See also https://(), https://()
[since 5.5] QImageIOHandler::Transformations QImageWriter::transformation() const▲
Returns the transformation and orientation the image has been set to written with.
This function was introduced in Qt 5.5.
See Also▲
See also https://()
bool QImageWriter::write(const QImage &image)▲
Writes the image image to the assigned device or file name. Returns true on success; otherwise returns false. If the operation fails, you can call https://() to find the type of error that occurred, or https://() to get a human readable description of the error.
See Also▲
See also https://(), https://(), https://()


