6.0.0-rc4Source

QTextBlock Class

The QTextBlock class provides a container for text fragments in a QTextDocument. More…

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

List of all members, including inherited members

Public Functions

QTextBlock()
boolisValid() const
QStringtext() const
intposition() const
intlength() const
boolcontains(int position) const
intblockNumber() const
QTextBlockFormatblockFormat() const
QTextCharFormatcharFormat() const
QList<QTextLayout::FormatRange>textFormats() const
QTextLayout *layout() const
const QTextDocument *document() const
QTextBlocknext() const
QTextBlockprevious() const

Detailed Description

A block is a paragraph of a document. It has a text(), a block format for the paragraph as a whole, and character formats for the parts of its text (textFormats()). next() leads through the document:

for (QTextBlock block = document.begin(); block.isValid(); block = block.next())
    qDebug() << block.blockNumber() << block.text();

A QTextBlock refers to its document: it is valid for as long as the document is, and until the document gets other contents.

See also QTextDocument.

Differences from Qt

A block gives its formats as a list of format ranges; there are no fragment iterators, no user data or states, no revisions, and blocks cannot be hidden. end() of a document is an invalid block.

Member Function Documentation

QTextBlock::QTextBlock()

Constructs an invalid block. Valid blocks come from a document.

QTextBlockFormat QTextBlock::blockFormat() const

Returns the format of the block as a paragraph: its alignment, margins, indentation and heading level.

int QTextBlock::blockNumber() const

Returns the position of the block among the blocks of its document, counted from 0, or -1 for an invalid block.

QTextCharFormat QTextBlock::charFormat() const

Returns the character format at the start of the block, which is empty if the text there has none of its own.

See also textFormats().

bool QTextBlock::contains(int position) const

Returns true if the position position of the document is in this block; otherwise returns false.

const QTextDocument * QTextBlock::document() const

Returns the document of the block, or nullptr.

bool QTextBlock::isValid() const

Returns true if this is a block of a document; otherwise returns false.

QTextLayout * QTextBlock::layout() const

Returns the layout of the block, with its lines as the document laid them out for its text width, or nullptr for an invalid block. The position of the layout is that of the block in the document.

int QTextBlock::length() const

Returns the length of the block: that of its text, and one for the paragraph separator.

See also position().

QTextBlock QTextBlock::next() const

Returns the block after this one, or an invalid block after the last.

See also previous().

int QTextBlock::position() const

Returns the position of the first character of the block in the document. Each block before it counts with its text and one more, for the paragraph separator.

See also length().

QTextBlock QTextBlock::previous() const

Returns the block before this one, or an invalid block before the first.

See also next().

QString QTextBlock::text() const

Returns the text of the block, without formats. A line break inside the block is the character U+2028.

QList<QTextLayout::FormatRange> QTextBlock::textFormats() const

Returns the character formats of the block: for each of them, the part of the text that it applies to.