6.0.0-rc4Source

QTextDocument Class

The QTextDocument class holds formatted text. More…

Header#include <QTextDocument>
CMaketarget_link_libraries(app PRIVATE Nt::Gui)
ModuleNt GUI C++ Classes
InheritsQObject

List of all members, including inherited members

Public Types

Public Functions

QTextDocument(QObject *parent = nullptr)
QTextDocument(const QString &text, QObject *parent = nullptr)
~QTextDocument()
boolisEmpty() const
virtual voidclear()
voidsetPlainText(const QString &text)
QStringtoPlainText() const
voidsetHtml(const QString &html)
QStringtoHtml() const
voidsetMetaInformation(MetaInformation info, const QString &string)
QStringmetaInformation(MetaInformation info) const
voidsetDefaultFont(const QFont &font)
QFontdefaultFont() const
voidsetDefaultTextOption(const QTextOption &option)
QTextOptiondefaultTextOption() const
voidsetTextWidth(qreal width)
qrealtextWidth() const
qrealidealWidth() const
QSizeFsize() const
qrealdocumentMargin() const
voidsetDocumentMargin(qreal margin)
intblockCount() const
intlineCount() const
intcharacterCount() const
QCharcharacterAt(int pos) const
QTextBlockbegin() const
QTextBlockend() const
QTextBlockfirstBlock() const
QTextBlocklastBlock() const
QTextBlockfindBlock(int pos) const
QTextBlockfindBlockByNumber(int blockNumber) const
voiddrawContents(QPainter *painter, const QRectF &rect = QRectF())
QAbstractTextDocumentLayout *documentLayout() const

Signals

Detailed Description

A QTextDocument is text in paragraphs, each with a format of its own and with formats for parts of its text: fonts, sizes, colors, links, headings and lists. It reads and writes a subset of HTML, lays itself out for a width, and draws itself:

QTextDocument document;
document.setHtml("<h2>Report</h2><p>Sales grew by <b>12&nbsp;%</b>; see the "
                 "<a href=\"details.html\">details</a>.</p>");
document.setTextWidth(300);
document.drawContents(&painter);

size() is the size that the document takes for textWidth(), and idealWidth() the width that it would like. documentLayout() tells what is where: the link under a point, with anchorAt(). begin() and the QTextBlock objects that it leads to give the paragraphs and their formats.

HTML

setHtml() understands these elements; others are ignored, their text is kept.

The style attribute sets color, background-color, font-weight, font-style, font-size, font-family, text-decoration and, for a paragraph, text-align and the margins. align does the alignment too. The usual character references are decoded.

See also QTextLayout, QTextCharFormat and QTextBlock.

Differences from Qt

The document is made as a whole, with setPlainText() or setHtml(): it has no QTextCursor, no undo and redo, and its contents are not edited in place. There are no tables, frames, images, style sheets, resources, Markdown, printing, searching (find()) or page sizes. The layout is the one of the document: setDocumentLayout() is not there, and QAbstractTextDocumentLayout is a small class that answers questions about it. The HTML that toHtml() writes is simpler than that of Qt. Lists do not nest by structure: a nested list is a list with a deeper indentation.

Member Type Documentation

enum QTextDocument::MetaInformation

What a document knows about itself.

ConstantDescription
QTextDocument::DocumentTitleThe title of the document, from the title element of its HTML.
QTextDocument::DocumentUrlThe URL that the document came from, for the application to set.

Member Function Documentation

[explicit] QTextDocument::QTextDocument(QObject *parent = nullptr)

Constructs an empty document with the given parent.

[explicit] QTextDocument::QTextDocument(const QString &text, QObject *parent = nullptr)

This is an overloaded function.

Constructs a document with the plain text text and the given parent.

[override] QTextDocument::~QTextDocument()

Destroys the document.

QTextBlock QTextDocument::begin() const

Returns the first block of the document.

See also end() and QTextBlock::next().

int QTextDocument::blockCount() const

Returns the number of blocks, that is of paragraphs, of the document. An empty document has one, which is empty.

QChar QTextDocument::characterAt(int pos) const

Returns the character at the position pos of the document. The paragraph separator at the end of a block is U+2029. Returns a null character if pos is out of range.

int QTextDocument::characterCount() const

Returns the number of characters of the document: those of its blocks, and one for each block, for the paragraph separator.

[virtual] void QTextDocument::clear()

Removes the contents of the document. Its font, its text width and its margin stay.

[signal] void QTextDocument::contentsChanged()

This signal is emitted when the contents of the document changed.

QFont QTextDocument::defaultFont() const

Returns the font of the text that has no font of its own.

See also setDefaultFont().

QTextOption QTextDocument::defaultTextOption() const

Returns the text option of the document.

See also setDefaultTextOption().

QAbstractTextDocumentLayout * QTextDocument::documentLayout() const

Returns the object that tells where things are in the document as it is laid out. The document owns it.

qreal QTextDocument::documentMargin() const

Returns the margin around the document, in pixels. The default is 4.

See also setDocumentMargin().

void QTextDocument::drawContents(QPainter *painter, const QRectF &rect = QRectF())

Draws the document with painter, with its top left corner at the origin of the painter. The text has the color of the pen of the painter where it has none of its own. With a valid rect, only the part of the document inside it is drawn.

QTextBlock QTextDocument::end() const

Returns an invalid block, which is what QTextBlock::next() of the last block returns.

See also begin().

QTextBlock QTextDocument::findBlock(int pos) const

Returns the block that has the position pos of the document, or an invalid block if pos is out of range.

See also findBlockByNumber().

QTextBlock QTextDocument::findBlockByNumber(int blockNumber) const

Returns the block at position blockNumber of the blocks, counted from 0, or an invalid block if there is none.

See also findBlock().

QTextBlock QTextDocument::firstBlock() const

Returns the first block of the document.

qreal QTextDocument::idealWidth() const

Returns the width that the document takes when no line is broken that does not have to be: that of its longest line, with the margins.

See also setTextWidth() and size().

bool QTextDocument::isEmpty() const

Returns true if the document has no text; otherwise returns false.

QTextBlock QTextDocument::lastBlock() const

Returns the last block of the document.

int QTextDocument::lineCount() const

Returns the number of lines of the document as it is laid out for its text width.

QString QTextDocument::metaInformation(MetaInformation info) const

Returns what the document knows about itself for info: its title, which setHtml() reads from the title element, or its URL.

See also setMetaInformation().

void QTextDocument::setDefaultFont(const QFont &font)

Sets the font of the text that has no font of its own to font.

See also defaultFont().

void QTextDocument::setDefaultTextOption(const QTextOption &option)

Sets how the lines of the document are broken to what option says. The alignment of a paragraph comes from its block format, not from option.

See also defaultTextOption().

void QTextDocument::setDocumentMargin(qreal margin)

Sets the margin around the document to margin pixels.

See also documentMargin().

void QTextDocument::setHtml(const QString &html)

Makes the HTML in html the contents of the document. The description of the class lists what is understood; what is not is left out, and its text kept. Sizes that are relative, as those of headings, are taken of the default font as it is now: set the font first.

See also toHtml(), setPlainText() and setDefaultFont().

void QTextDocument::setMetaInformation(MetaInformation info, const QString &string)

Sets what the document knows about itself for info to string.

See also metaInformation().

void QTextDocument::setPlainText(const QString &text)

Makes text, without formats, the contents of the document: a paragraph for each of its lines.

See also toPlainText() and setHtml().

void QTextDocument::setTextWidth(qreal width)

Sets the width that the document is laid out for to width pixels: lines are broken to fit into it. With a width that is not positive, which is the default, lines are only broken where the text has line breaks, and the document is as wide as its longest line.

See also textWidth(), size() and idealWidth().

QSizeF QTextDocument::size() const

Returns the size of the document as it is laid out: textWidth() wide, or as wide as its longest line if no text width is set, and as high as its lines and margins.

See also setTextWidth().

qreal QTextDocument::textWidth() const

Returns the width that the document is laid out for, or -1 if none is set.

See also setTextWidth().

QString QTextDocument::toHtml() const

Returns the contents of the document as HTML that setHtml() reads back to the same text and formats.

See also setHtml() and toPlainText().

QString QTextDocument::toPlainText() const

Returns the text of the document without its formats, with a line break between paragraphs and for each line break inside one.

See also setPlainText() and toHtml().