QTextDocumentWriter Class
QTextDocumentWriter 类提供了一个与格式无关的接口,用于将QTextDocument 写入文件或其他设备。更多
头文件: | #include <QTextDocumentWriter> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
- 所有成员(包括继承成员)的列表
- QTextDocumentWriter 是富文本处理 API 的一部分。
公共函数
QTextDocumentWriter() | |
QTextDocumentWriter(QIODevice *device, const QByteArray &format) | |
QTextDocumentWriter(const QString &fileName, const QByteArray &format = QByteArray()) | |
~QTextDocumentWriter() | |
QIODevice * | device() const |
QString | fileName() const |
QByteArray | format() const |
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() |
详细说明
要写入文档,请使用文件名或设备对象构建一个 QTextDocumentWriter 对象,并指定要写入的文档格式。您可以稍后使用setFormat() 构造写入器并设置格式。
调用write() 将文档写入设备。如果文档写入成功,该函数将返回true
。但是,如果在写入文档时发生错误,函数将返回 false。
调用supportedDocumentFormats() 查看 QTextDocumentWriter 可写入的格式列表。
由于支持的输出格式的功能差异很大,写入器只需为每种格式输出适当的对象子集。这通常包括文档中包含的格式化文本和图像。
成员函数文档
QTextDocumentWriter::QTextDocumentWriter()
构造一个空的 QTextDocumentWriter 对象。在写入之前,必须调用setFormat() 设置文档格式,然后调用setDevice() 或setFileName() 。
QTextDocumentWriter::QTextDocumentWriter(QIODevice *device, const QByteArray &format)
构造一个 QTextDocumentWriter 对象,以format 指定的文档格式写入给定的device 。
[explicit]
QTextDocumentWriter::QTextDocumentWriter(const QString &fileName, const QByteArray &format = QByteArray())
构造一个 QTextDocumentWriter 对象,该对象将使用format 指定的文档格式写入名称为fileName 的文件。如果未提供format ,QTextDocumentWriter 将通过检查fileName 的扩展名来检测文档格式。
[noexcept]
QTextDocumentWriter::~QTextDocumentWriter()
销毁QTextDocumentWriter 对象。
QIODevice *QTextDocumentWriter::device() const
返回当前分配的设备,如果没有分配设备,则返回nullptr
。
另请参阅 setDevice().
QString QTextDocumentWriter::fileName() const
如果当前分配的设备是QFile ,或已调用setFileName() ,则该函数返回要写入的文件名。在其他情况下,该函数返回空字符串。
另请参阅 setFileName() 和setDevice()。
QByteArray QTextDocumentWriter::format() const
返回编写文件时使用的格式。
另请参阅 setFormat()。
void QTextDocumentWriter::setDevice(QIODevice *device)
将写入器的设备设置为device 。如果已经设置了设备,旧设备将被移除,否则保持不变。
如果设备尚未打开,QTextDocumentWriter 将调用 open() 试图以WriteOnly 模式打开设备。
注意: 这对某些设备无效,如QProcess 、QTcpSocket 和QUdpSocket ,在这些设备上,打开前需要进行一些配置。
另请参阅 device() 和setFileName()。
void QTextDocumentWriter::setFileName(const QString &fileName)
设置要写入fileName 的文件名。在内部,QTextDocumentWriter 将创建QFile 并在WriteOnly 模式下打开,并在写入文档时使用该文件。
void QTextDocumentWriter::setFormat(const QByteArray &format)
设置用于将文档写入format 的格式。format 是不区分大小写的文本字符串。例如
QTextDocumentWriter writer; writer.setFormat("odf"); // same as writer.setFormat("ODF");
您可以调用supportedDocumentFormats() 查看QTextDocumentWriter 支持的全部格式列表。
另请参阅 format()。
[static]
QList<QByteArray> QTextDocumentWriter::supportedDocumentFormats()
返回QTextDocumentWriter 支持的文档格式列表。
默认情况下,Qt 可以编写以下格式:
格式 | 描述 |
---|---|
纯文本 | 纯文本 |
超文本标记语言 | 超文本标记语言 |
标记符 | 标记语言(CommonMark 或 GitHub 方言) |
ODF | 开放文档格式 |
另请参见 setFormat().
bool QTextDocumentWriter::write(const QTextDocument *document)
将给定的document 写入指定的设备或文件,如果成功则返回true
,否则返回false
。
bool QTextDocumentWriter::write(const QTextDocumentFragment &fragment)
将fragment 指定的文件片段写入指定的设备或文件,如果成功则返回true
,否则返回false
。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.