Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QTextDocumentWriter Class Reference

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

#include <qtextdocumentwriter.h>

+ Collaboration diagram for QTextDocumentWriter:

Public Member Functions

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

Static Public Member Functions

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

Detailed Description

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

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 18 of file qtextdocumentwriter.h.

Constructor & Destructor Documentation

◆ 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 78 of file qtextdocumentwriter.cpp.

◆ 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 87 of file qtextdocumentwriter.cpp.

References QTextDocumentWriterPrivate::device, device(), QTextDocumentWriterPrivate::format, and format().

+ Here is the call graph for this function:

◆ QTextDocumentWriter() [3/3]

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

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 100 of file qtextdocumentwriter.cpp.

References QTextDocumentWriterPrivate::deleteDevice.

◆ ~QTextDocumentWriter()

QTextDocumentWriter::~QTextDocumentWriter ( )

Member Function Documentation

◆ device()

QIODevice * QTextDocumentWriter::device ( ) const

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

Definition at line 169 of file qtextdocumentwriter.cpp.

References QTextDocumentWriterPrivate::device.

Referenced by QTextDocumentWriter(), and setDevice().

+ Here is the caller graph for this function:

◆ 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 195 of file qtextdocumentwriter.cpp.

References QTextDocumentWriterPrivate::device, file, and QFile::fileName().

Referenced by setFileName().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ format()

QByteArray QTextDocumentWriter::format ( ) const

Returns the format used for writing documents.

See also
setFormat()

Definition at line 137 of file qtextdocumentwriter.cpp.

References QTextDocumentWriterPrivate::format.

Referenced by QTextDocumentWriter(), and setFormat().

+ Here is the caller graph for this function:

◆ 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 \l {QIODeviceBase::}{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 156 of file qtextdocumentwriter.cpp.

References QTextDocumentWriterPrivate::deleteDevice, QTextDocumentWriterPrivate::device, and device().

Referenced by setFileName().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 \l {QIODeviceBase::}{WriteOnly} mode, and use this file when writing the document.

See also
fileName(), setDevice()

Definition at line 182 of file qtextdocumentwriter.cpp.

References QTextDocumentWriterPrivate::deleteDevice, fileName(), and setDevice().

+ Here is the call graph for this function:

◆ 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 127 of file qtextdocumentwriter.cpp.

References QTextDocumentWriterPrivate::format, and format().

Referenced by src_gui_text_qtextdocumentwriter::wrapper().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ supportedDocumentFormats()

QList< QByteArray > QTextDocumentWriter::supportedDocumentFormats ( )
static

Returns the list of document formats supported by QTextDocumentWriter.

By default, Qt can write the following formats:

\table \header

  • Format
  • Description \row
  • plaintext
  • Plain text \row
  • HTML
  • HyperText Markup Language \row
  • markdown
  • Markdown (CommonMark or GitHub dialects) \row
  • ODF
  • OpenDocument Format \endtable
See also
setFormat()

Definition at line 292 of file qtextdocumentwriter.cpp.

◆ 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 205 of file qtextdocumentwriter.cpp.

References QIODevice::close(), QTextDocumentWriterPrivate::device, file, QFile::fileName(), QTextDocumentWriterPrivate::format, QByteArray::isEmpty(), QIODevice::isWritable(), QIODevice::open(), qWarning, QFileInfo::suffix(), QString::toLatin1(), QByteArray::toLower(), QString::toLower(), QIODevice::write(), and QIODeviceBase::WriteOnly.

Referenced by write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 267 of file qtextdocumentwriter.cpp.

References QTextDocumentFragmentPrivate::doc, and write().

+ Here is the call graph for this function:

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