QTextDocumentFragment Class

The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument. More...

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

Note: All functions in this class are reentrant.

Public Functions

QTextDocumentFragment(const QTextDocumentFragment &other)
QTextDocumentFragment(const QTextCursor &cursor)
QTextDocumentFragment(const QTextDocument *document)
QTextDocumentFragment &operator=(const QTextDocumentFragment &other)
bool isEmpty() const
QString toHtml() const
QString toPlainText() const

Static Public Members

QTextDocumentFragment fromHtml(const QString &text, const QTextDocument *resourceProvider = nullptr)
QTextDocumentFragment fromPlainText(const QString &plainText)

Detailed Description

A QTextDocumentFragment is a fragment of rich text, that can be inserted into a QTextDocument. A document fragment can be created from a QTextDocument, from a QTextCursor's selection, or from another document fragment. Document fragments can also be created by the static functions, fromPlainText() and fromHtml().

The contents of a document fragment can be obtained as plain text by using the toPlainText() function, or it can be obtained as HTML with toHtml().

Member Function Documentation

QTextDocumentFragment::QTextDocumentFragment(const QTextDocumentFragment &other)

Copy constructor. Creates a copy of the other fragment.

QTextDocumentFragment::QTextDocumentFragment(const QTextCursor &cursor)

Creates a QTextDocumentFragment from the cursor's selection. If the cursor doesn't have a selection, the created fragment is empty.

See also isEmpty() and QTextCursor::selection().

QTextDocumentFragment::QTextDocumentFragment(const QTextDocument *document)

Converts the given document into a QTextDocumentFragment. Note that the QTextDocumentFragment only stores the document contents, not meta information like the document's title.


Constructs an empty QTextDocumentFragment.

See also isEmpty().

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

Assigns the other fragment to this fragment.


Destroys the document fragment.

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

Returns a QTextDocumentFragment based on the arbitrary piece of HTML in the given text. The formatting is preserved as much as possible; for example, "<b>bold</b>" will become a document fragment with the text "bold" with a bold character format.

If the provided HTML contains references to external resources such as imported style sheets, then they will be loaded through the resourceProvider.

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

Returns a document fragment that contains the given plainText.

When inserting such a fragment into a QTextDocument the current char format of the QTextCursor used for insertion is used as format for the text.

bool QTextDocumentFragment::isEmpty() const

Returns true if the fragment is empty; otherwise returns false.

QString QTextDocumentFragment::toHtml() const

Returns the contents of the document fragment as HTML.

See also toPlainText() and QTextDocument::toHtml().

QString QTextDocumentFragment::toPlainText() const

Returns the document fragment's text as plain text (i.e. with no formatting information).

See also toHtml().