QTextDocumentWriter 類

QTextDocumentWriter 類提供格式無關接口,用於寫入 QTextDocument 到文件或其它設備。 更多...

頭: #include <QTextDocumentWriter>
qmake: QT += gui
Since: Qt 4.5

該類在 Qt 4.5 引入。

公共函數

QTextDocumentWriter (const QString & fileName , const QByteArray & format = QByteArray())
QTextDocumentWriter (QIODevice * device , const QByteArray & format )
QTextDocumentWriter ()
~QTextDocumentWriter ()
QTextCodec * codec () const
QIODevice * device () const
QString fileName () const
QByteArray format () const
void setCodec (QTextCodec * codec )
void setDevice (QIODevice * device )
void setFileName (const QString & fileName )
void setFormat (const QByteArray & format )
bool write (const QTextDocument * document )
bool write (const QTextDocumentFragment & fragment )

靜態公共成員

QList<QByteArray> supportedDocumentFormats ()

詳細描述

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.

調用 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.

調用 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.

成員函數文檔編製

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 。若 format is not provided, QTextDocumentWriter will detect the document format by inspecting the extension of fileName .

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

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

QTextDocumentWriter:: QTextDocumentWriter ()

Constructs an empty QTextDocumentWriter object. Before writing, you must call setFormat () to set a document format, then setDevice () 或 setFileName ().

QTextDocumentWriter:: ~QTextDocumentWriter ()

銷毀 QTextDocumentWriter 對象。

QTextCodec *QTextDocumentWriter:: codec () const

Returns the codec that is currently assigned to the writer.

另請參閱 setCodec ().

QIODevice *QTextDocumentWriter:: device () const

Returns the device currently assigned, or nullptr 若沒有設備被賦值。

另請參閱 setDevice ().

QString QTextDocumentWriter:: fileName () const

若目前賦值設備是 QFile ,或者若 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.

另請參閱 setFileName () 和 setDevice ().

QByteArray QTextDocumentWriter:: format () const

返迴用於寫文檔的格式。

另請參閱 setFormat ().

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.

另請參閱 codec ().

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.

若設備尚未打開, QTextDocumentWriter will attempt to open the device in QIODevice::WriteOnly mode by calling open().

注意: This will not work for certain devices, such as QProcess , QTcpSocket and QUdpSocket , where some configuration is required before the device can be opened.

另請參閱 device () 和 setFileName ().

void QTextDocumentWriter:: setFileName (const QString & fileName )

Sets the name of the file to be written to fileName . Internally, QTextDocumentWriter 將創建 QFile and open it in QIODevice::WriteOnly mode, and use this file when writing the document.

另請參閱 fileName () 和 setDevice ().

void QTextDocumentWriter:: setFormat (const QByteArray & format )

Sets the format used to write documents to the format 指定。 format is a case insensitive text string. For example:

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

可以調用 supportedDocumentFormats () 瞭解完整格式列錶 QTextDocumentWriter 支持。

另請參閱 format ().

[static] QList < QByteArray > QTextDocumentWriter:: supportedDocumentFormats ()

返迴文檔格式列錶支持通過 QTextDocumentWriter .

默認情況下,Qt 可以寫入下列格式:

格式 描述
plaintext 純文本
HTML HTML (超文本標記語言)
markdown Markdown (CommonMark 或 GitHub 方言)
ODF ODF (開放文檔格式)

另請參閱 setFormat ().

bool QTextDocumentWriter:: write (const QTextDocument * document )

寫入給定 document 到賦值設備 (或文件) 並返迴 true 若成功;否則返迴 false .

bool QTextDocumentWriter:: write (const QTextDocumentFragment & fragment )

Writes the document fragment specified by fragment 到賦值設備 (或文件) 並返迴 true 若成功;否則返迴 false .