Qt 4.8
Public Types | Public Functions | Static Public Functions | Properties | List of all members
QImageWriter Class Reference

The QImageWriter class provides a format independent interface for writing images to files or other devices. More...

#include <qimagewriter.h>

Public Types

enum  ImageWriterError { UnknownError, DeviceError, UnsupportedFormatError }
 This enum describes errors that can occur when writing images with QImageWriter. More...
 

Public Functions

bool canWrite () const
 Returns true if QImageWriter can write the image; i.e., the image format is supported and the assigned device is open for reading. More...
 
int compression () const
 Returns the compression of the image. More...
 
QString description () const
 Use QImageReader::text() instead. More...
 
QIODevicedevice () const
 Returns the device currently assigned to QImageWriter, or 0 if no device has been assigned. More...
 
ImageWriterError error () const
 Returns the type of error that last occurred. More...
 
QString errorString () const
 Returns a human readable description of the last error that occurred. More...
 
QString fileName () const
 If the currently assigned device is a QFile, or if setFileName() has been called, this function returns the name of the file QImageWriter writes to. More...
 
QByteArray format () const
 Returns the format QImageWriter uses for writing images. More...
 
float gamma () const
 Returns the gamma level of the image. More...
 
 QImageWriter ()
 Constructs an empty QImageWriter object. More...
 
 QImageWriter (QIODevice *device, const QByteArray &format)
 Constructs a QImageWriter object using the device device and image format format. More...
 
 QImageWriter (const QString &fileName, const QByteArray &format=QByteArray())
 Constructs a QImageWriter objects that will write to a file with the name fileName, using the image format format. More...
 
int quality () const
 Returns the quality level of the image. More...
 
void setCompression (int compression)
 This is an image format specific function that set the compression of an image. More...
 
void setDescription (const QString &description)
 Use setText() instead. More...
 
void setDevice (QIODevice *device)
 Sets QImageWriter's device to device. More...
 
void setFileName (const QString &fileName)
 Sets the file name of QImageWriter to fileName. More...
 
void setFormat (const QByteArray &format)
 Sets the format QImageWriter will use when writing images, to format. More...
 
void setGamma (float gamma)
 This is an image format specific function that sets the gamma level of the image to gamma. More...
 
void setQuality (int quality)
 This is an image format specific function that sets the quality level of the image to quality. More...
 
void setText (const QString &key, const QString &text)
 Sets the image text associated with the key key to text. More...
 
bool supportsOption (QImageIOHandler::ImageOption option) const
 Returns true if the writer supports option; otherwise returns false. More...
 
bool write (const QImage &image)
 Writes the image image to the assigned device or file name. More...
 
 ~QImageWriter ()
 Destructs the QImageWriter object. More...
 

Static Public Functions

static QList< QByteArraysupportedImageFormats ()
 Returns the list of image formats supported by QImageWriter. More...
 

Properties

QImageWriterPrivated
 

Detailed Description

The QImageWriter class provides a format independent interface for writing images to files or other devices.

Note
This class or function is reentrant.

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() or QPixmap::save() instead.

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

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.

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

See also
QImageReader, QImageIOHandler, QImageIOPlugin

Definition at line 59 of file qimagewriter.h.

Enumerations

◆ ImageWriterError

This enum describes errors that can occur when writing images with QImageWriter.

  • DeviceError QImageWriter encountered a device error when writing the image data. Consult your device for more details on what went wrong.
  • UnsupportedFormatError Qt does not support the requested image format.
  • UnknownError An unknown error occurred. If you get this value after calling write(), it is most likely caused by a bug in QImageWriter.
Enumerator
UnknownError 
DeviceError 
UnsupportedFormatError 

Definition at line 62 of file qimagewriter.h.

Constructors and Destructors

◆ QImageWriter() [1/3]

QImageWriter::QImageWriter ( )

Constructs an empty QImageWriter object.

Before writing, you must call setFormat() to set an image format, then setDevice() or setFileName().

Definition at line 298 of file qimagewriter.cpp.

299  : d(new QImageWriterPrivate(this))
300 {
301 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109

◆ QImageWriter() [2/3]

QImageWriter::QImageWriter ( QIODevice device,
const QByteArray format 
)
explicit

Constructs a QImageWriter object using the device device and image format format.

Definition at line 307 of file qimagewriter.cpp.

308  : d(new QImageWriterPrivate(this))
309 {
310  d->device = device;
311  d->format = format;
312 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QByteArray format() const
Returns the format QImageWriter uses for writing images.
QIODevice * device() const
Returns the device currently assigned to QImageWriter, or 0 if no device has been assigned...

◆ QImageWriter() [3/3]

QImageWriter::QImageWriter ( const QString fileName,
const QByteArray format = QByteArray() 
)
explicit

Constructs a QImageWriter objects that will write to a file with the name fileName, using the image format format.

If format is not provided, QImageWriter will detect the image format by inspecting the extension of fileName.

Definition at line 320 of file qimagewriter.cpp.

321  : d(new QImageWriterPrivate(this))
322 {
323  QFile *file = new QFile(fileName);
324  d->device = file;
325  d->deleteDevice = true;
326  d->format = format;
327 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QByteArray format() const
Returns the format QImageWriter uses for writing images.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65

◆ ~QImageWriter()

QImageWriter::~QImageWriter ( )

Destructs the QImageWriter object.

Definition at line 332 of file qimagewriter.cpp.

333 {
334  if (d->deleteDevice)
335  delete d->device;
336  delete d->handler;
337  delete d;
338 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QImageIOHandler * handler

Functions

◆ canWrite()

bool QImageWriter::canWrite ( ) const

Returns true if QImageWriter can write the image; i.e., the image format is supported and the assigned device is open for reading.

See also
write(), setDevice(), setFormat()

Definition at line 578 of file qimagewriter.cpp.

Referenced by write().

579 {
580  if (d->device && !d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
583  QLatin1String("Unsupported image format"));
584  return false;
585  }
586  if (d->device && !d->device->isOpen())
588  if (!d->device || !d->device->isWritable()) {
591  QLatin1String("Device not writable"));
592  return false;
593  }
594  return true;
595 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
Definition: qiodevice.cpp:558
#define QT_TRANSLATE_NOOP(scope, x)
Marks the string literal sourceText for dynamic translation in the given context; i...
Definition: qglobal.h:2487
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QImageIOHandler * handler
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
The QImageWriter class provides a format independent interface for writing images to files or other d...
Definition: qimagewriter.h:59
QImageWriter::ImageWriterError imageWriterError
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570

◆ compression()

int QImageWriter::compression ( ) const

Returns the compression of the image.

See also
setCompression()

Definition at line 473 of file qimagewriter.cpp.

Referenced by setCompression().

474 {
475  return d->compression;
476 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109

◆ description()

QString QImageWriter::description ( ) const

Use QImageReader::text() instead.

Returns the description of the image.

See also
setDescription()

Definition at line 536 of file qimagewriter.cpp.

Referenced by setDescription().

537 {
538  return d->description;
539 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109

◆ device()

QIODevice * QImageWriter::device ( ) const

Returns the device currently assigned to QImageWriter, or 0 if no device has been assigned.

Definition at line 394 of file qimagewriter.cpp.

Referenced by QImageWriter(), and setDevice().

395 {
396  return d->device;
397 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109

◆ error()

QImageWriter::ImageWriterError QImageWriter::error ( ) const

Returns the type of error that last occurred.

See also
ImageWriterError, errorString()

Definition at line 632 of file qimagewriter.cpp.

633 {
634  return d->imageWriterError;
635 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QImageWriter::ImageWriterError imageWriterError

◆ errorString()

QString QImageWriter::errorString ( ) const

Returns a human readable description of the last error that occurred.

See also
error()

Definition at line 642 of file qimagewriter.cpp.

643 {
644  return d->errorString;
645 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109

◆ fileName()

QString QImageWriter::fileName ( ) const

If the currently assigned device is a QFile, or if 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 is returned.

See also
setFileName(), setDevice()

Definition at line 420 of file qimagewriter.cpp.

421 {
422  QFile *file = qobject_cast<QFile *>(d->device);
423  return file ? file->fileName() : QString();
424 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
T qobject_cast(QObject *object)
Definition: qobject.h:375
QString fileName() const
Returns the name set by setFileName() or to the QFile constructors.
Definition: qfile.cpp:470
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65

◆ format()

QByteArray QImageWriter::format ( ) const

Returns the format QImageWriter uses for writing images.

See also
setFormat()

Definition at line 361 of file qimagewriter.cpp.

Referenced by QImageWriter(), and setFormat().

362 {
363  return d->format;
364 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109

◆ gamma()

float QImageWriter::gamma ( ) const

Returns the gamma level of the image.

See also
setGamma()

Definition at line 498 of file qimagewriter.cpp.

Referenced by setGamma().

499 {
500  return d->gamma;
501 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109

◆ quality()

int QImageWriter::quality ( ) const

Returns the quality level of the image.

See also
setQuality()

Definition at line 447 of file qimagewriter.cpp.

Referenced by setQuality().

448 {
449  return d->quality;
450 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109

◆ setCompression()

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
compression()

Definition at line 463 of file qimagewriter.cpp.

464 {
466 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
int compression() const
Returns the compression of the image.

◆ setDescription()

void QImageWriter::setDescription ( const QString description)

Use setText() instead.

This is an image format specific function that sets the description of the image to description. For image formats that do not support setting the description, this value is ignored.

The contents of description depends on the image format.

See also
description()

Definition at line 519 of file qimagewriter.cpp.

520 {
522 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QString description() const
Use QImageReader::text() instead.

◆ setDevice()

void QImageWriter::setDevice ( QIODevice device)

Sets QImageWriter's device to device.

If a device has already been set, the old device is removed from QImageWriter and is otherwise left unchanged.

If the device is not already open, 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.

See also
device(), setFileName()

Definition at line 379 of file qimagewriter.cpp.

Referenced by QXlibMime::mimeConvertToFormat(), setFileName(), and QX11Data::xdndMimeConvertToFormat().

380 {
381  if (d->device && d->deleteDevice)
382  delete d->device;
383 
384  d->device = device;
385  d->deleteDevice = false;
386  delete d->handler;
387  d->handler = 0;
388 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QImageIOHandler * handler
QIODevice * device() const
Returns the device currently assigned to QImageWriter, or 0 if no device has been assigned...

◆ setFileName()

void QImageWriter::setFileName ( const QString fileName)

Sets the file name of QImageWriter to fileName.

Internally, QImageWriter will create a QFile and open it in QIODevice::WriteOnly mode, and use this file when writing images.

See also
fileName(), setDevice()

Definition at line 405 of file qimagewriter.cpp.

406 {
407  setDevice(new QFile(fileName));
408  d->deleteDevice = true;
409 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
void setDevice(QIODevice *device)
Sets QImageWriter&#39;s device to device.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65

◆ setFormat()

void QImageWriter::setFormat ( const QByteArray format)

Sets the format QImageWriter 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 supportedImageFormats() for the full list of formats QImageWriter supports.

See also
format()

Definition at line 351 of file qimagewriter.cpp.

Referenced by QXlibMime::mimeConvertToFormat(), and QX11Data::xdndMimeConvertToFormat().

352 {
353  d->format = format;
354 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QByteArray format() const
Returns the format QImageWriter uses for writing images.

◆ setGamma()

void QImageWriter::setGamma ( float  gamma)

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.

See also
quality()

Definition at line 488 of file qimagewriter.cpp.

489 {
490  d->gamma = gamma;
491 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
float gamma() const
Returns the gamma level of the image.

◆ setQuality()

void QImageWriter::setQuality ( int  quality)

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

See also
quality()

Definition at line 437 of file qimagewriter.cpp.

Referenced by QPdfEnginePrivate::addImage(), compressHelper(), QImageData::doImageIO(), and QPixmap::doImageIO().

438 {
439  d->quality = quality;
440 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
int quality() const
Returns the quality level of the image.

◆ setText()

void QImageWriter::setText ( const QString key,
const QString text 
)

Sets the image text associated with the key key to text.

Since
4.1

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().

Support for this option is implemented through QImageIOHandler::Description.

See also
QImage::setText(), QImageReader::text()

Definition at line 565 of file qimagewriter.cpp.

566 {
567  if (!d->description.isEmpty())
568  d->description += QLatin1String("\n\n");
569  d->description += key.simplified() + QLatin1String(": ") + text.simplified();
570 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415

◆ supportedImageFormats()

QList< QByteArray > QImageWriter::supportedImageFormats ( )
static

Returns the list of image formats supported by QImageWriter.

By default, Qt can write the following formats:

Format Description
BMP Windows Bitmap
JPG Joint Photographic Experts Group
JPEG Joint Photographic Experts Group
PNG Portable Network Graphics
PPM Portable Pixmap
TIFF Tagged Image File Format
XBM X11 Bitmap
XPM X11 Pixmap

Reading and writing SVG files is supported through Qt's QtSvg Module{SVG Module}.

Note that the QApplication instance must be created before this function is called.

See also
setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin

Definition at line 704 of file qimagewriter.cpp.

Referenced by QPdfEnginePrivate::addImage(), compressHelper(), and imageWriteMimeFormats().

705 {
706  QSet<QByteArray> formats;
707  formats << "bmp";
708 #ifndef QT_NO_IMAGEFORMAT_PPM
709  formats << "ppm";
710 #endif
711 #ifndef QT_NO_IMAGEFORMAT_XBM
712  formats << "xbm";
713 #endif
714 #ifndef QT_NO_IMAGEFORMAT_XPM
715  formats << "xpm";
716 #endif
717 #ifndef QT_NO_IMAGEFORMAT_PNG
718  formats << "png";
719 #endif
720 #ifndef QT_NO_IMAGEFORMAT_JPEG
721  formats << "jpg" << "jpeg";
722 #endif
723 #ifndef QT_NO_IMAGEFORMAT_MNG
724  formats << "mng";
725 #endif
726 #ifndef QT_NO_IMAGEFORMAT_TIFF
727  formats << "tif" << "tiff";
728 #endif
729 #ifdef QT_BUILTIN_GIF_READER
730  formats << "gif";
731 #endif
732 
733 #ifndef QT_NO_LIBRARY
734  QFactoryLoader *l = loader();
735  QStringList keys = l->keys();
736  for (int i = 0; i < keys.count(); ++i) {
737  QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
738  if (plugin && (plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanWrite) != 0)
739  formats << keys.at(i).toLatin1();
740  }
741 #endif // QT_NO_LIBRARY
742 
743  QList<QByteArray> sortedFormats;
744  for (QSet<QByteArray>::ConstIterator it = formats.constBegin(); it != formats.constEnd(); ++it)
745  sortedFormats << *it;
746 
747  qSort(sortedFormats);
748  return sortedFormats;
749 }
T qobject_cast(QObject *object)
Definition: qobject.h:375
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const =0
Returns the capabilities on the plugin, based on the data in device and the format format...
#define it(className, varName)
const_iterator constEnd() const
Definition: qset.h:171
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QStringList keys
QStringList keys() const
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
The QImageIOPlugin class defines an interface for writing an image format plugin. ...
QObject * instance(const QString &key) const
QFactoryLoader * l
const_iterator constBegin() const
Definition: qset.h:168

◆ supportsOption()

bool QImageWriter::supportsOption ( QImageIOHandler::ImageOption  option) const

Returns true if the writer supports option; otherwise returns false.

Since
4.2

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()).

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
QImageReader::supportsOption(), setFormat()

Definition at line 667 of file qimagewriter.cpp.

668 {
669  if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
672  QLatin1String("Unsupported image format"));
673  return false;
674  }
675 
676  return d->handler->supportsOption(option);
677 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
#define QT_TRANSLATE_NOOP(scope, x)
Marks the string literal sourceText for dynamic translation in the given context; i...
Definition: qglobal.h:2487
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QImageIOHandler * handler
The QImageWriter class provides a format independent interface for writing images to files or other d...
Definition: qimagewriter.h:59
QImageWriter::ImageWriterError imageWriterError
virtual bool supportsOption(ImageOption option) const
Returns true if the QImageIOHandler supports the option option; otherwise returns false...

◆ write()

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 error() to find the type of error that occurred, or errorString() to get a human readable description of the error.

See also
canWrite(), error(), errorString()

Definition at line 606 of file qimagewriter.cpp.

Referenced by QPdfEnginePrivate::addImage(), compressHelper(), QImageData::doImageIO(), QPixmap::doImageIO(), QXlibMime::mimeConvertToFormat(), operator<<(), QSystemTrayIconPrivate::showMessage_sys(), QTextOdfWriter::writeInlineCharacter(), and QX11Data::xdndMimeConvertToFormat().

607 {
608  if (!canWrite())
609  return false;
610 
619 
620  if (!d->handler->write(image))
621  return false;
622  if (QFile *file = qobject_cast<QFile *>(d->device))
623  file->flush();
624  return true;
625 }
QImageWriterPrivate * d
Definition: qimagewriter.h:109
QImageIOHandler * handler
virtual bool write(const QImage &image)
Writes the image image to the assigned device.
virtual void setOption(ImageOption option, const QVariant &value)
Sets the option option with the value value.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
bool canWrite() const
Returns true if QImageWriter can write the image; i.e., the image format is supported and the assigne...
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
virtual bool supportsOption(ImageOption option) const
Returns true if the QImageIOHandler supports the option option; otherwise returns false...

Properties

◆ d

QImageWriterPrivate* QImageWriter::d
private

The documentation for this class was generated from the following files: