QTextDocumentFragment Class

QTextDocumentFragment 类表示来自QTextDocument一段格式化文本。

头文件: #include <QTextDocumentFragment>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

注意:该类中的所有函数都是可重入的

公共函数

QTextDocumentFragment()
QTextDocumentFragment(const QTextCursor &cursor)
QTextDocumentFragment(const QTextDocument *document)
QTextDocumentFragment(const QTextDocumentFragment &other)
~QTextDocumentFragment()
bool isEmpty() const
QString toHtml() const
(since 6.4) QString toMarkdown(QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub) const
QString toPlainText() const
(since 6.4) QString toRawText() const
QTextDocumentFragment &operator=(const QTextDocumentFragment &other)

静态公共成员

QTextDocumentFragment fromHtml(const QString &text, const QTextDocument *resourceProvider = nullptr)
(since 6.4) QTextDocumentFragment fromMarkdown(const QString &markdown, QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub)
QTextDocumentFragment fromPlainText(const QString &plainText)

详细说明

QTextDocumentFragment 是一个富文本片段,可以插入到QTextDocument 中。文档片段可以从QTextDocumentQTextCursor 的选中内容或其他文档片段中创建。文档片段也可以通过静态函数fromPlainText() 和fromHtml() 创建。

文档片段的内容可以通过toRawText() 函数获取原始文本,通过toPlainText() 获取 ASCII 格式,通过toHtml() 获取 HTML 格式,或通过toMarkdown() 获取 Markdown 格式。

成员函数文档

QTextDocumentFragment::QTextDocumentFragment()

构造一个空的 QTextDocumentFragment。

另请参阅 isEmpty().

[explicit] QTextDocumentFragment::QTextDocumentFragment(const QTextCursor &cursor)

cursor 的选区创建一个 QTextDocumentFragment。如果游标没有选区,则创建的片段为空。

另请参阅 isEmpty() 和QTextCursor::selection() 。

[explicit] QTextDocumentFragment::QTextDocumentFragment(const QTextDocument *document)

将给定的document 转换为 QTextDocumentFragment。请注意,QTextDocumentFragment 只存储文档内容,不存储文档标题等元信息。

QTextDocumentFragment::QTextDocumentFragment(const QTextDocumentFragment &other)

复制构造函数。创建other 片段的副本。

[noexcept] QTextDocumentFragment::~QTextDocumentFragment()

销毁文档片段。

[static] QTextDocumentFragment QTextDocumentFragment::fromHtml(const QString &text, const QTextDocument *resourceProvider = nullptr)

根据给定text 中的任意 HTML 片段返回QTextDocumentFragment 。格式将尽可能保留;例如,"<b>粗体</b>"将成为包含 "粗体 "文本的文档片段,并带有粗体字符格式。

如果所提供的 HTML 包含外部资源引用(如导入的样式表),则将通过resourceProvider 加载这些引用。

[static, since 6.4] QTextDocumentFragment QTextDocumentFragment::fromMarkdown(const QString &markdown, QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub)

根据给定的markdown 文本和指定的features 返回QTextDocumentFragment 。默认为 GitHub 方言。

格式将尽可能保留;例如,**bold** 将成为包含文本 "bold "的文档片段,并带有粗体字符样式。

注意: 不支持加载外部资源。

此功能在 Qt 6.4 中引入。

[static] QTextDocumentFragment QTextDocumentFragment::fromPlainText(const QString &plainText)

返回包含给定plainText 的文档片段。

将该片段插入QTextDocument 时,插入所用QTextCursor 的当前字符格式将用作文本格式。

bool QTextDocumentFragment::isEmpty() const

如果片段为空,则返回true ;否则返回false

QString QTextDocumentFragment::toHtml() const

以 HTML 格式返回文档片段的内容。

另请参阅 toPlainText()、toMarkdown() 和QTextDocument::toHtml()。

[since 6.4] QString QTextDocumentFragment::toMarkdown(QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub) const

以 Markdown 格式返回文档片段的内容,并使用指定的features 。默认为 GitHub 方言。

此函数在 Qt 6.4 中引入。

另请参阅 toPlainText() 和QTextDocument::toMarkdown()。

QString QTextDocumentFragment::toPlainText() const

该函数的返回值与toRawText() 相同,但会用 ASCII 替代码替换某些 unicode 字符。特别是,无分隔空格(U+00A0)将被常规空格(U+0020)替换,段落(U+2029)和行(U+2028)分隔符将被换行(U+000A)替换。如果您需要精确的文档内容,请使用toRawText() 代替。

另请参阅 toHtml()、toMarkdown() 和toRawText()。

[since 6.4] QString QTextDocumentFragment::toRawText() const

将文档片段的文本返回为原始文本(即不含格式化信息)。

此函数在 Qt 6.4 中引入。

另请参阅 toHtml()、toMarkdown() 和toPlainText()。

QTextDocumentFragment &QTextDocumentFragment::operator=(const QTextDocumentFragment &other)

other 片段分配给此片段。

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