このページでは

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 に挿入できるリッチテキストのフラグメントです。ドキュメントフラグメントは、QTextDocument 、QTextCursor の選択範囲、または別のドキュメントフラグメントから作成できます。ドキュメントフラグメントは、静的関数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>bold</b>」は、テキスト「bold」が太字で表示されるドキュメントフラグメントになります。

指定された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

指定されたfeatures を使用して、ドキュメントフラグメントの内容をMarkdown形式で返します。デフォルトはGitHubダイアレクトです。

この関数は Qt 6.4 で導入されました。

toPlainText() およびQTextDocument::toMarkdown()も参照してください 。

QString QTextDocumentFragment::toPlainText() const

この関数は、toRawText() と同じ結果を返しますが、一部の Unicode 文字を ASCII 形式の代替文字に置き換えます。 具体的には、非改行スペース (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 フラグメントをこのフラグメントに割り当てます。

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