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

The QTextDocumentWriter class provides a format-independent interface for writing a QTextDocument to files or other devices. More...

#include <qtextdocumentwriter.h>

Public Functions

QTextCodeccodec () const
 Returns the codec that is currently assigned to the writer. More...
 
QIODevicedevice () const
 Returns the device currently assigned, or 0 if no device has been assigned. 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 to be written to. More...
 
QByteArray format () const
 Returns the format used for writing documents. More...
 
 QTextDocumentWriter ()
 Constructs an empty QTextDocumentWriter object. More...
 
 QTextDocumentWriter (QIODevice *device, const QByteArray &format)
 Constructs a QTextDocumentWriter object to write to the given device in the document format specified by format. More...
 
 QTextDocumentWriter (const QString &fileName, const QByteArray &format=QByteArray())
 Constructs an QTextDocumentWriter object that will write to a file with the name fileName, using the document format specified by format. More...
 
void setCodec (QTextCodec *codec)
 Sets the codec for this stream to codec. More...
 
void setDevice (QIODevice *device)
 Sets the writer's device to the device specified. More...
 
void setFileName (const QString &fileName)
 Sets the name of the file to be written to fileName. More...
 
void setFormat (const QByteArray &format)
 Sets the format used to write documents to the format specified. More...
 
bool write (const QTextDocument *document)
 Writes the given document to the assigned device or file and returns true if successful; otherwise returns false. More...
 
bool write (const QTextDocumentFragment &fragment)
 Writes the document fragment specified by fragment to the assigned device or file and returns true if successful; otherwise returns false. More...
 
 ~QTextDocumentWriter ()
 Destroys the QTextDocumentWriter object. More...
 

Static Public Functions

static QList< QByteArraysupportedDocumentFormats ()
 Returns the list of document formats supported by QTextDocumentWriter. More...
 

Properties

QTextDocumentWriterPrivated
 

Detailed Description

The QTextDocumentWriter class provides a format-independent interface for writing a QTextDocument to files or other devices.

Since
4.5

To write a document, construct a QTextDocumentWriter object with either a file name or a device object, and specify the document format to be written. You can construct a writer and set the format using setFormat() later.

Call write() to write the document to the device. If the document is successfully written, this function returns true. However, if an error occurs when writing the document, it will return false.

Call supportedDocumentFormats() for a list of formats that QTextDocumentWriter can write.

Since the capabilities of the supported output formats vary considerably, the writer simply outputs the appropriate subset of objects for each format. This typically includes the formatted text and images contained in a document.

Definition at line 58 of file qtextdocumentwriter.h.

Constructors and Destructors

◆ QTextDocumentWriter() [1/3]

QTextDocumentWriter::QTextDocumentWriter ( )

Constructs an empty QTextDocumentWriter object.

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

Definition at line 121 of file qtextdocumentwriter.cpp.

122  : d(new QTextDocumentWriterPrivate(this))
123 {
124 }
QTextDocumentWriterPrivate * d

◆ QTextDocumentWriter() [2/3]

QTextDocumentWriter::QTextDocumentWriter ( QIODevice device,
const QByteArray format 
)

Constructs a QTextDocumentWriter object to write to the given device in the document format specified by format.

Definition at line 130 of file qtextdocumentwriter.cpp.

131  : d(new QTextDocumentWriterPrivate(this))
132 {
133  d->device = device;
134  d->format = format;
135 }
QIODevice * device() const
Returns the device currently assigned, or 0 if no device has been assigned.
QTextDocumentWriterPrivate * d
QByteArray format() const
Returns the format used for writing documents.

◆ QTextDocumentWriter() [3/3]

QTextDocumentWriter::QTextDocumentWriter ( const QString fileName,
const QByteArray format = QByteArray() 
)

Constructs an QTextDocumentWriter object that will write to a file with the name fileName, using the document format specified by format.

If format is not provided, QTextDocumentWriter will detect the document format by inspecting the extension of fileName.

Definition at line 143 of file qtextdocumentwriter.cpp.

144  : d(new QTextDocumentWriterPrivate(this))
145 {
146  QFile *file = new QFile(fileName);
147  d->device = file;
148  d->deleteDevice = true;
149  d->format = format;
150 }
QTextDocumentWriterPrivate * d
QByteArray format() const
Returns the format used for writing documents.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65

◆ ~QTextDocumentWriter()

QTextDocumentWriter::~QTextDocumentWriter ( )

Destroys the QTextDocumentWriter object.

Definition at line 155 of file qtextdocumentwriter.cpp.

156 {
157  if (d->deleteDevice)
158  delete d->device;
159  delete d;
160 }
QTextDocumentWriterPrivate * d

Functions

◆ codec()

QTextCodec * QTextDocumentWriter::codec ( ) const

Returns the codec that is currently assigned to the writer.

Definition at line 338 of file qtextdocumentwriter.cpp.

Referenced by setCodec().

339 {
340  return d->codec;
341 }
QTextDocumentWriterPrivate * d

◆ device()

QIODevice * QTextDocumentWriter::device ( ) const

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

Definition at line 215 of file qtextdocumentwriter.cpp.

Referenced by QTextDocumentWriter(), and setDevice().

216 {
217  return d->device;
218 }
QTextDocumentWriterPrivate * d

◆ fileName()

QString QTextDocumentWriter::fileName ( ) const

If the currently assigned device is a QFile, or if setFileName() has been called, this function returns the name of the file to be written to.

In all other cases, it returns an empty string.

See also
setFileName(), setDevice()

Definition at line 239 of file qtextdocumentwriter.cpp.

240 {
241  QFile *file = qobject_cast<QFile *>(d->device);
242  return file ? file->fileName() : QString();
243 }
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
QTextDocumentWriterPrivate * d
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65

◆ format()

QByteArray QTextDocumentWriter::format ( ) const

Returns the format used for writing documents.

See also
setFormat()

Definition at line 183 of file qtextdocumentwriter.cpp.

Referenced by QTextDocumentWriter(), and setFormat().

184 {
185  return d->format;
186 }
QTextDocumentWriterPrivate * d

◆ setCodec()

void QTextDocumentWriter::setCodec ( QTextCodec codec)

Sets the codec for this stream to codec.

The codec is used for encoding any data that is written. By default, QTextDocumentWriter uses UTF-8.

Definition at line 325 of file qtextdocumentwriter.cpp.

326 {
327  if (codec == 0)
328  codec = QTextCodec::codecForName("UTF-8");
329  Q_ASSERT(codec);
330  d->codec = codec;
331 }
QTextCodec * codec() const
Returns the codec that is currently assigned to the writer.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QTextDocumentWriterPrivate * d
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...

◆ setDevice()

void QTextDocumentWriter::setDevice ( QIODevice device)

Sets the writer's device to the device specified.

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

If the device is not already open, QTextDocumentWriter will attempt to open the device in QIODevice::WriteOnly mode by calling open().

Note
This will not work for certain devices, such as QProcess, QTcpSocket and QUdpSocket, where some configuration is required before the device can be opened.
See also
device(), setFileName()

Definition at line 202 of file qtextdocumentwriter.cpp.

Referenced by setFileName().

203 {
204  if (d->device && d->deleteDevice)
205  delete d->device;
206 
207  d->device = device;
208  d->deleteDevice = false;
209 }
QIODevice * device() const
Returns the device currently assigned, or 0 if no device has been assigned.
QTextDocumentWriterPrivate * d

◆ setFileName()

void QTextDocumentWriter::setFileName ( const QString fileName)

Sets the name of the file to be written to fileName.

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

See also
fileName(), setDevice()

Definition at line 226 of file qtextdocumentwriter.cpp.

227 {
228  setDevice(new QFile(fileName));
229  d->deleteDevice = true;
230 }
QTextDocumentWriterPrivate * d
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
void setDevice(QIODevice *device)
Sets the writer&#39;s device to the device specified.

◆ setFormat()

void QTextDocumentWriter::setFormat ( const QByteArray format)

Sets the format used to write documents to the format specified.

format is a case insensitive text string. For example:

writer.setFormat("odf"); // same as writer.setFormat("ODF");

You can call supportedDocumentFormats() for the full list of formats QTextDocumentWriter supports.

See also
format()

Definition at line 173 of file qtextdocumentwriter.cpp.

174 {
175  d->format = format;
176 }
QTextDocumentWriterPrivate * d
QByteArray format() const
Returns the format used for writing documents.

◆ supportedDocumentFormats()

QList< QByteArray > QTextDocumentWriter::supportedDocumentFormats ( )
static

Returns the list of document formats supported by QTextDocumentWriter.

By default, Qt can write the following formats:

Format Description
plaintext Plain text
HTML HyperText Markup Language
ODF OpenDocument Format
See also
setFormat()

Definition at line 358 of file qtextdocumentwriter.cpp.

359 {
360  QList<QByteArray> answer;
361  answer << "plaintext";
362 
363 #ifndef QT_NO_TEXTHTMLPARSER
364  answer << "HTML";
365 #endif
366 #ifndef QT_NO_TEXTODFWRITER
367  answer << "ODF";
368 #endif // QT_NO_TEXTODFWRITER
369 
370  qSort(answer);
371  return answer;
372 }
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177

◆ write() [1/2]

bool QTextDocumentWriter::write ( const QTextDocument document)

Writes the given document to the assigned device or file and returns true if successful; otherwise returns false.

Definition at line 249 of file qtextdocumentwriter.cpp.

Referenced by QTextEditMimeData::setup(), and write().

250 {
252 
253  if (d->device && d->format.isEmpty()) {
254  // if there's no format, see if device is a file, and if so, find
255  // the file suffix
256  if (QFile *file = qobject_cast<QFile *>(d->device))
257  suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1();
258  }
259 
261 
262 #ifndef QT_NO_TEXTODFWRITER
263  if (format == "odf" || format == "opendocumentformat" || format == "odt") {
264  QTextOdfWriter writer(*document, d->device);
265 #ifndef QT_NO_TEXTCODEC
266  writer.setCodec(d->codec);
267 #endif
268  return writer.writeAll();
269  }
270 #endif // QT_NO_TEXTODFWRITER
271 
272 #ifndef QT_NO_TEXTHTMLPARSER
273  if (format == "html" || format == "htm") {
274  if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {
275  qWarning() << "QTextDocumentWriter::write: the device can not be opened for writing";
276  return false;
277  }
278  QTextStream ts(d->device);
279 #ifndef QT_NO_TEXTCODEC
280  ts.setCodec(d->codec);
281  ts << document->toHtml(d->codec->name());
282 #endif
283  d->device->close();
284  return true;
285  }
286 #endif
287  if (format == "txt" || format == "plaintext") {
288  if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {
289  qWarning() << "QTextDocumentWriter::write: the device can not be opened for writing";
290  return false;
291  }
292  QTextStream ts(d->device);
293 #ifndef QT_NO_TEXTCODEC
294  ts.setCodec(d->codec);
295 #endif
296  ts << document->toPlainText();
297  d->device->close();
298  return true;
299  }
300 
301  return false;
302 }
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
Definition: qiodevice.cpp:558
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QByteArray toLower() const
Returns a lowercase copy of the byte array.
QString toPlainText() const
Returns the plain text contained in the document.
Q_CORE_EXPORT void qWarning(const char *,...)
QTextDocumentWriterPrivate * d
virtual QByteArray name() const =0
QTextCodec subclasses must reimplement this function.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
QByteArray suffix
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
QString toHtml(const QByteArray &encoding=QByteArray()) const
Returns a string containing an HTML representation of the document.

◆ write() [2/2]

bool QTextDocumentWriter::write ( const QTextDocumentFragment fragment)

Writes the document fragment specified by fragment to the assigned device or file and returns true if successful; otherwise returns false.

Definition at line 308 of file qtextdocumentwriter.cpp.

309 {
310  if (fragment.d == 0)
311  return false; // invalid fragment.
312  QTextDocument *doc = fragment.d->doc;
313  if (doc)
314  return write(doc);
315  return false;
316 }
QTextDocumentFragmentPrivate * d
bool write(const QTextDocument *document)
Writes the given document to the assigned device or file and returns true if successful; otherwise re...
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...

Properties

◆ d

QTextDocumentWriterPrivate* QTextDocumentWriter::d
private

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