Qt 4.8
Classes | Public Types | Public Functions | Properties | Friends | List of all members
QTextBlock Class Reference

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

#include <qtextobject.h>

Classes

class  iterator
 The QTextBlock::iterator class provides an iterator for reading the contents of a QTextBlock. More...
 

Public Types

typedef iterator Iterator
 Qt-style synonym for QTextBlock::iterator. More...
 

Public Functions

iterator begin () const
 Returns a text block iterator pointing to the beginning of the text block. More...
 
QTextBlockFormat blockFormat () const
 Returns the QTextBlockFormat that describes block-specific properties. More...
 
int blockFormatIndex () const
 Returns an index into the document's internal list of block formats for the text block's format. More...
 
int blockNumber () const
 Returns the number of this block, or -1 if the block is invalid. More...
 
QTextCharFormat charFormat () const
 Returns the QTextCharFormat that describes the block's character format. More...
 
int charFormatIndex () const
 Returns an index into the document's internal list of character formats for the text block's character format. More...
 
void clearLayout ()
 Clears the QTextLayout that is used to lay out and display the block's contents. More...
 
bool contains (int position) const
 Returns true if the given position is located within the text block; otherwise returns false. More...
 
QTextDocumentPrivatedocHandle () const
 
const QTextDocumentdocument () const
 Returns the text document this text block belongs to, or 0 if the text block does not belong to any document. More...
 
iterator end () const
 Returns a text block iterator pointing to the end of the text block. More...
 
int firstLineNumber () const
 Returns the first line number of this block, or -1 if the block is invalid. More...
 
int fragmentIndex () const
 
bool isValid () const
 Returns true if this text block is valid; otherwise returns false. More...
 
bool isVisible () const
 Returns true if the block is visible; otherwise returns false. More...
 
QTextLayoutlayout () const
 Returns the QTextLayout that is used to lay out and display the block's contents. More...
 
int length () const
 Returns the length of the block in characters. More...
 
int lineCount () const
 
QTextBlock next () const
 Returns the text block in the document after this block, or an empty text block if this is the last one. More...
 
bool operator!= (const QTextBlock &o) const
 Returns true if this text block is different from the other text block. More...
 
bool operator< (const QTextBlock &o) const
 Returns true if this text block occurs before the other text block in the document. More...
 
QTextBlockoperator= (const QTextBlock &o)
 Assigns the other text block to this text block. More...
 
bool operator== (const QTextBlock &o) const
 Returns true if this text block is the same as the other text block. More...
 
int position () const
 Returns the index of the block's first character within the document. More...
 
QTextBlock previous () const
 Returns the text block in the document before this block, or an empty text block if this is the first one. More...
 
 QTextBlock (QTextDocumentPrivate *priv, int b)
 
 QTextBlock ()
 
 QTextBlock (const QTextBlock &o)
 Copies the other text block's attributes to this text block. More...
 
int revision () const
 Returns the blocks revision. More...
 
void setLineCount (int count)
 
void setRevision (int rev)
 Sets a blocks revision to rev. More...
 
void setUserData (QTextBlockUserData *data)
 Attaches the given data object to the text block. More...
 
void setUserState (int state)
 Stores the specified state integer value in the text block. More...
 
void setVisible (bool visible)
 Sets the block's visibility to visible. More...
 
QString text () const
 Returns the block's contents as plain text. More...
 
Qt::LayoutDirection textDirection () const
 Returns the resolved text direction. More...
 
QTextListtextList () const
 If the block represents a list item, returns the list that the item belongs to; otherwise returns 0. More...
 
QTextBlockUserDatauserData () const
 Returns a pointer to a QTextBlockUserData object if previously set with setUserData() or a null pointer. More...
 
int userState () const
 Returns the integer value previously set with setUserState() or -1. More...
 

Properties

int n
 
QTextDocumentPrivatep
 

Friends

class QSyntaxHighlighter
 
class QTextDocumentPrivate
 
class QTextLayout
 

Detailed Description

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

Note
This class or function is reentrant.

A text block encapsulates a block or paragraph of text in a QTextDocument. QTextBlock provides read-only access to the block/paragraph structure of QTextDocuments. It is mainly of use if you want to implement your own layouts for the visual representation of a QTextDocument, or if you want to iterate over a document and write out the contents in your own custom format.

Text blocks are created by their parent documents. If you need to create a new text block, or modify the contents of a document while examining its contents, use the cursor-based interface provided by QTextCursor instead.

Each text block is located at a specific position() in a document(). The contents of the block can be obtained by using the text() function. The length() function determines the block's size within the document (including formatting characters). The visual properties of the block are determined by its text layout(), its charFormat(), and its blockFormat().

The next() and previous() functions enable iteration over consecutive valid blocks in a document under the condition that the document is not modified by other means during the iteration process. Note that, although blocks are returned in sequence, adjacent blocks may come from different places in the document structure. The validity of a block can be determined by calling isValid().

QTextBlock provides comparison operators to make it easier to work with blocks: operator==() compares two block for equality, operator!=() compares two blocks for inequality, and operator<() determines whether a block precedes another in the same document.

qtextblock-sequence.png
See also
QTextBlockFormat QTextCharFormat QTextFragment

Definition at line 199 of file qtextobject.h.

Typedefs

◆ Iterator

Qt-style synonym for QTextBlock::iterator.

Definition at line 275 of file qtextobject.h.

Constructors and Destructors

◆ QTextBlock() [1/3]

QTextBlock::QTextBlock ( QTextDocumentPrivate priv,
int  b 
)
inline
Warning
This function is not part of the public interface.

Definition at line 203 of file qtextobject.h.

203 : p(priv), n(b) {}
QTextDocumentPrivate * p
Definition: qtextobject.h:287

◆ QTextBlock() [2/3]

QTextBlock::QTextBlock ( )
inline
Warning
This function is not part of the public interface.

Definition at line 204 of file qtextobject.h.

204 : p(0), n(0) {}
QTextDocumentPrivate * p
Definition: qtextobject.h:287

◆ QTextBlock() [3/3]

QTextBlock::QTextBlock ( const QTextBlock o)
inline

Copies the other text block's attributes to this text block.

Definition at line 205 of file qtextobject.h.

205 : p(o.p), n(o.n) {}
QTextDocumentPrivate * p
Definition: qtextobject.h:287

Functions

◆ begin()

QTextBlock::iterator QTextBlock::begin ( ) const

Returns a text block iterator pointing to the beginning of the text block.

See also
end()

Definition at line 1573 of file qtextobject.cpp.

Referenced by QTextControlPrivate::activateLinkUnderCursor(), QTextHtmlExporter::emitBlock(), QTextHtmlExporter::emitBlockAttributes(), QTextHtmlExporter::emitFrame(), QTextControl::findNextPrevAnchor(), and QTextOdfWriter::writeBlock().

1574 {
1575  if (!p || !n)
1576  return iterator();
1577 
1578  int pos = position();
1579  int len = length() - 1; // exclude the fragment that holds the paragraph separator
1580  int b = p->fragmentMap().findNode(pos);
1581  int e = p->fragmentMap().findNode(pos+len);
1582  return iterator(p, b, e, b);
1583 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
int position() const
Returns the index of the block&#39;s first character within the document.
int length() const
Returns the length of the block in characters.
uint findNode(int k, uint field=0) const
const FragmentMap & fragmentMap() const

◆ blockFormat()

QTextBlockFormat QTextBlock::blockFormat ( ) const

Returns the QTextBlockFormat that describes block-specific properties.

See also
charFormat()

Definition at line 1182 of file qtextobject.cpp.

Referenced by QTextList::add(), QTextCopyHelper::appendFragment(), QTextCursorPrivate::blockFormat(), QTextCursor::blockFormat(), QAccessibleTextWidget::characterRect(), QTextCopyHelper::copy(), QTextDocumentLayoutPrivate::drawBlock(), QTextDocumentLayoutPrivate::drawFlow(), QTextDocumentLayoutPrivate::drawListItem(), QTextHtmlExporter::emitBlock(), QTextHtmlExporter::emitBlockAttributes(), isEmptyBlockBeforeTable(), QTextList::itemText(), QTextDocumentLayoutPrivate::layoutFlow(), QPlainTextEdit::paintEvent(), and QTextList::remove().

1183 {
1184  if (!p || !n)
1185  return QTextFormat().toBlockFormat();
1186 
1188 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
The QTextFormat class provides formatting information for a QTextDocument.
Definition: qtextformat.h:129
QTextFormatCollection * formatCollection()
QTextBlockFormat toBlockFormat() const
Returns this format as a block format.
QTextBlockFormat blockFormat(int index) const
Definition: qtextformat_p.h:83
const BlockMap & blockMap() const
Fragment * fragment(uint index)

◆ blockFormatIndex()

int QTextBlock::blockFormatIndex ( ) const

Returns an index into the document's internal list of block formats for the text block's format.

See also
QTextDocument::allFormats()

Definition at line 1196 of file qtextobject.cpp.

Referenced by QTextOdfWriter::writeBlock().

1197 {
1198  if (!p || !n)
1199  return -1;
1200 
1201  return p->blockMap().fragment(n)->format;
1202 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
Fragment * fragment(uint index)

◆ blockNumber()

int QTextBlock::blockNumber ( ) const

Returns the number of this block, or -1 if the block is invalid.

Since
4.4
\sa QTextCursor::blockNumber()

Definition at line 1512 of file qtextobject.cpp.

Referenced by QPlainTextEditControl::blockBoundingRect(), QTextCursor::blockNumber(), QPlainTextDocumentLayout::documentChanged(), QScriptEdit::extraAreaPaintEvent(), QPlainTextDocumentLayout::layoutBlock(), QPlainTextEditPrivate::pageUpDown(), QPlainTextEditPrivate::setTopBlock(), and QPlainTextEditPrivate::setTopLine().

1513 {
1514  if (!p || !n)
1515  return -1;
1516  return p->blockMap().position(n, 1);
1517 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
uint position(uint node, uint field=0) const
const BlockMap & blockMap() const

◆ charFormat()

QTextCharFormat QTextBlock::charFormat ( ) const

Returns the QTextCharFormat that describes the block's character format.

The block's character format is used when inserting text into an empty block.

See also
blockFormat()

Definition at line 1211 of file qtextobject.cpp.

Referenced by QTextCopyHelper::appendFragment(), QTextCursor::blockCharFormat(), QTextHtmlExporter::emitBlock(), QTextHtmlExporter::emitBlockAttributes(), QPlainTextDocumentLayout::layoutBlock(), QTextDocumentLayoutPrivate::layoutBlock(), and QScriptLine::setDefaultHeight().

1212 {
1213  if (!p || !n)
1214  return QTextFormat().toCharFormat();
1215 
1217 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:85
The QTextFormat class provides formatting information for a QTextDocument.
Definition: qtextformat.h:129
QTextFormatCollection * formatCollection()
QTextCharFormat toCharFormat() const
Returns this format as a character format.
int charFormatIndex() const
Returns an index into the document&#39;s internal list of character formats for the text block&#39;s characte...

◆ charFormatIndex()

int QTextBlock::charFormatIndex ( ) const

Returns an index into the document's internal list of character formats for the text block's character format.

See also
QTextDocument::allFormats()

Definition at line 1225 of file qtextobject.cpp.

1226 {
1227  if (!p || !n)
1228  return -1;
1229 
1230  return p->blockCharFormatIndex(n);
1231 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
int blockCharFormatIndex(int node) const

◆ clearLayout()

void QTextBlock::clearLayout ( )

Clears the QTextLayout that is used to lay out and display the block's contents.

Since
4.4
See also
layout()

Definition at line 1167 of file qtextobject.cpp.

Referenced by QTextDocumentLayout::documentChanged(), and QPlainTextDocumentLayout::documentChanged().

1168 {
1169  if (!p || !n)
1170  return;
1171 
1172  const QTextBlockData *b = p->blockMap().fragment(n);
1173  if (b->layout)
1174  b->layout->clearLayout();
1175 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
QTextLayout * layout
const BlockMap & blockMap() const
Fragment * fragment(uint index)
void clearLayout()
Clears the line information in the layout.

◆ contains()

bool QTextBlock::contains ( int  position) const

Returns true if the given position is located within the text block; otherwise returns false.

Definition at line 1126 of file qtextobject.cpp.

Referenced by QTextDocumentLayoutPrivate::drawBlock(), QTextDocumentLayoutPrivate::drawFlow(), and QPlainTextEdit::paintEvent().

1127 {
1128  if (!p || !n)
1129  return false;
1130 
1131  int pos = p->blockMap().position(n);
1132  int len = p->blockMap().size(n);
1133  return position >= pos && position < pos + len;
1134 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
int position() const
Returns the index of the block&#39;s first character within the document.
uint position(uint node, uint field=0) const
const BlockMap & blockMap() const
uint size(uint node, uint field=0) const

◆ docHandle()

QTextDocumentPrivate * QTextBlock::docHandle ( ) const
inline

◆ document()

const QTextDocument * QTextBlock::document ( ) const

Returns the text document this text block belongs to, or 0 if the text block does not belong to any document.

Definition at line 1313 of file qtextobject.cpp.

Referenced by QPlainTextEditPrivate::ensureVisible(), QSyntaxHighlighter::rehighlightBlock(), and QPlainTextDocumentLayoutPrivate::relayout().

1314 {
1315  return p ? p->document() : 0;
1316 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
QTextDocument * document()

◆ end()

QTextBlock::iterator QTextBlock::end ( ) const

Returns a text block iterator pointing to the end of the text block.

See also
begin() next() previous()

Definition at line 1591 of file qtextobject.cpp.

Referenced by QTextControl::findNextPrevAnchor().

1592 {
1593  if (!p || !n)
1594  return iterator();
1595 
1596  int pos = position();
1597  int len = length() - 1; // exclude the fragment that holds the paragraph separator
1598  int b = p->fragmentMap().findNode(pos);
1599  int e = p->fragmentMap().findNode(pos+len);
1600  return iterator(p, b, e, e);
1601 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
int position() const
Returns the index of the block&#39;s first character within the document.
int length() const
Returns the length of the block in characters.
uint findNode(int k, uint field=0) const
const FragmentMap & fragmentMap() const

◆ firstLineNumber()

int QTextBlock::firstLineNumber ( ) const

Returns the first line number of this block, or -1 if the block is invalid.

Since
4.5
Unless the layout supports it, the line number is identical to the block number.

\sa QTextBlock::blockNumber()

Definition at line 1531 of file qtextobject.cpp.

Referenced by QPlainTextEditPrivate::_q_adjustScrollbars(), QPlainTextEditControl::blockBoundingRect(), QPlainTextEditPrivate::setTopBlock(), and QPlainTextEditPrivate::setTopLine().

1532 {
1533  if (!p || !n)
1534  return -1;
1535  return p->blockMap().position(n, 2);
1536 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
uint position(uint node, uint field=0) const
const BlockMap & blockMap() const

◆ fragmentIndex()

int QTextBlock::fragmentIndex ( ) const
inline
Warning
This function is not part of the public interface.

Definition at line 284 of file qtextobject.h.

284 { return n; }

◆ isValid()

bool QTextBlock::isValid ( ) const
inline

Returns true if this text block is valid; otherwise returns false.

Definition at line 208 of file qtextobject.h.

Referenced by QPlainTextEditPrivate::_q_adjustScrollbars(), QPlainTextEditControl::blockBoundingRect(), QTextDocumentLayout::blockBoundingRect(), QPlainTextDocumentLayout::blockBoundingRect(), QAccessibleTextWidget::characterRect(), QAccessibleTextEdit::childAt(), QTextCursor::columnNumber(), currentTextLine(), QTextControlPrivate::cursorMoveKeyEvent(), QTextDocumentLayout::documentChanged(), QPlainTextDocumentLayout::documentChanged(), QTextDocumentLayoutPrivate::drawFrame(), QPlainTextDocumentLayout::ensureBlockLayout(), QScriptEdit::extraAreaPaintEvent(), QTextDocument::find(), QTextControl::findNextPrevAnchor(), QScriptEdit::gotoLine(), QPlainTextEditControl::hitTest(), isEmptyBlockAfterTable(), isEmptyBlockBeforeTable(), isLineSeparatorBlockAfterTable(), QPlainTextDocumentLayout::layoutBlock(), QTextDocumentLayoutPrivate::layoutFlow(), QTextCursorPrivate::movePosition(), QTextEngine::nextLogicalPosition(), QPlainTextEditPrivate::pageUpDown(), QPlainTextEdit::paintEvent(), QSyntaxHighlighter::previousBlockState(), qTextBlockAt(), qTextBlockPosition(), QAccessibleTextEdit::rect(), QTextControlPrivate::rectForPosition(), QSyntaxHighlighterPrivate::reformatBlock(), QSyntaxHighlighterPrivate::reformatBlocks(), QSyntaxHighlighter::rehighlightBlock(), QPlainTextDocumentLayoutPrivate::relayout(), QTextControl::selectionRect(), setBlockCharFormatHelper(), QTextDocumentPrivate::setBlockFormat(), QTextDocumentPrivate::setCharFormat(), QAccessibleTextEdit::setText(), QPlainTextEditPrivate::setTopBlock(), QTextLine::textLength(), QTextDocumentPrivate::undoRedo(), and QTextEngine::validate().

208 { return p != 0 && n != 0; }
QTextDocumentPrivate * p
Definition: qtextobject.h:287

◆ isVisible()

bool QTextBlock::isVisible ( ) const

Returns true if the block is visible; otherwise returns false.

Since
4.4
See also
setVisible()

Definition at line 1472 of file qtextobject.cpp.

Referenced by QPlainTextEditPrivate::_q_adjustScrollbars(), QPlainTextEditControl::blockBoundingRect(), QPlainTextDocumentLayout::blockBoundingRect(), QScriptEdit::extraAreaPaintEvent(), QPlainTextDocumentLayout::layoutBlock(), QTextCursor::movePosition(), QPlainTextEdit::paintEvent(), and QPlainTextDocumentLayoutPrivate::relayout().

1473 {
1474  if (!p || !n)
1475  return true;
1476 
1477  const QTextBlockData *b = p->blockMap().fragment(n);
1478  return !b->hidden;
1479 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
Fragment * fragment(uint index)

◆ layout()

QTextLayout * QTextBlock::layout ( ) const

Returns the QTextLayout that is used to lay out and display the block's contents.

Note that the returned QTextLayout object can only be modified from the documentChanged implementation of a QAbstractTextDocumentLayout subclass. Any changes applied from the outside cause undefined behavior.

See also
clearLayout()

Definition at line 1146 of file qtextobject.cpp.

Referenced by QPlainTextEditPrivate::_q_adjustScrollbars(), QSyntaxHighlighterPrivate::applyFormatChanges(), QTextDocumentLayout::blockBoundingRect(), QPlainTextDocumentLayout::blockBoundingRect(), QTextCursorPrivate::blockLayout(), QPlainTextDocumentLayout::blockWidth(), QAccessibleTextWidget::characterRect(), currentTextLine(), QTextControlPrivate::cursorMoveKeyEvent(), QPlainTextDocumentLayout::documentChanged(), QTextDocumentLayoutPrivate::drawBlock(), QTextDocumentLayoutPrivate::drawFrame(), QTextDocumentLayoutPrivate::drawListItem(), QPlainTextDocumentLayout::ensureBlockLayout(), flowPosition(), QPlainTextEditControl::hitTest(), QTextDocumentLayoutPrivate::hitTest(), QTextControlPrivate::inputMethodEvent(), QPlainTextDocumentLayout::layoutBlock(), QTextDocumentLayoutPrivate::layoutBlock(), QTextDocumentLayoutPrivate::layoutFlow(), QTextDocumentPrivate::leftCursorPosition(), QTextDocumentPrivate::nextCursorPosition(), QPlainTextEditPrivate::pageUpDown(), QPlainTextEdit::paintEvent(), QDeclarativeTextEdit::positionAt(), QTextDocumentLayout::positionInlineObject(), QTextDocumentPrivate::previousCursorPosition(), QTextControlPrivate::rectForPosition(), QPlainTextDocumentLayoutPrivate::relayout(), QTextDocumentPrivate::rightCursorPosition(), QTextControl::selectionRect(), and QPlainTextEditPrivate::verticalOffset().

1147 {
1148  if (!p || !n)
1149  return 0;
1150 
1151  const QTextBlockData *b = p->blockMap().fragment(n);
1152  if (!b->layout)
1153  b->layout = new QTextLayout(*this);
1154  return b->layout;
1155 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
QTextLayout * layout
const BlockMap & blockMap() const
Fragment * fragment(uint index)
friend class QTextLayout
Definition: qtextobject.h:290

◆ length()

int QTextBlock::length ( ) const

Returns the length of the block in characters.

Note
The length returned includes all formatting characters, for example, newline.
See also
text() charFormat() blockFormat()

Definition at line 1114 of file qtextobject.cpp.

Referenced by QSyntaxHighlighterPrivate::applyFormatChanges(), QTextCursor::atBlockEnd(), QTextDocumentLayout::blockBoundingRect(), QTextCursor::charFormat(), QTextDocumentLayoutPrivate::drawBlock(), QTextHtmlExporter::emitBlock(), QTextDocument::find(), QTextControl::findNextPrevAnchor(), QTextDocumentLayoutPrivate::hitTest(), QTextControl::inputMethodQuery(), isEmptyBlockAfterTable(), isEmptyBlockBeforeTable(), isLineSeparatorBlockAfterTable(), QTextEngine::itemize(), QTextControlPrivate::keyPressEvent(), QTextDocumentLayoutPrivate::layoutBlock(), QTextBlockGroupPrivate::markBlocksDirty(), QTextTable::mergeCells(), QTextCursorPrivate::movePosition(), QTextCursor::movePosition(), QTextDocumentPrivate::nextCursorPosition(), QTextEngine::nextLogicalPosition(), QPlainTextEdit::paintEvent(), QTextDocumentLayout::positionInlineObject(), QSyntaxHighlighterPrivate::reformatBlock(), QSyntaxHighlighterPrivate::reformatBlocks(), QTextCursor::select(), QTextDocumentPrivate::setBlockFormat(), QTextEdit::setText(), QTextDocumentPrivate::undoRedo(), and QTextOdfWriter::writeBlock().

1115 {
1116  if (!p || !n)
1117  return 0;
1118 
1119  return p->blockMap().size(n);
1120 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
uint size(uint node, uint field=0) const

◆ lineCount()

int QTextBlock::lineCount ( ) const
Since
4.5

Returns the line count. Not all document layouts support this feature.

See also
setLineCount()

Definition at line 1559 of file qtextobject.cpp.

1560 {
1561  if (!p || !n)
1562  return -1;
1563  return p->blockMap().size(n, 2);
1564 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
uint size(uint node, uint field=0) const

◆ next()

QTextBlock QTextBlock::next ( ) const

Returns the text block in the document after this block, or an empty text block if this is the last one.

Note that the next block may be in a different frame or table to this block.

See also
previous() begin() end()

Definition at line 1612 of file qtextobject.cpp.

Referenced by QPlainTextEditControl::blockBoundingRect(), QPlainTextDocumentLayout::blockBoundingRect(), QTextControlPrivate::cursorMoveKeyEvent(), QTextDocumentLayout::documentChanged(), QPlainTextDocumentLayout::documentChanged(), QScriptEdit::extraAreaPaintEvent(), QTextDocument::find(), QTextControl::findNextPrevAnchor(), QPlainTextEditControl::hitTest(), QPlainTextDocumentLayout::layoutBlock(), QTextCursorPrivate::movePosition(), QTextCursor::movePosition(), QPlainTextEditPrivate::pageUpDown(), QPlainTextEdit::paintEvent(), qTextBlockAt(), QSyntaxHighlighterPrivate::reformatBlocks(), QPlainTextDocumentLayoutPrivate::relayout(), setBlockCharFormatHelper(), QTextDocumentPrivate::setBlockFormat(), QTextDocumentPrivate::setCharFormat(), and QTextEngine::validate().

1613 {
1614  if (!isValid() || !p->blockMap().isValid(n))
1615  return QTextBlock();
1616 
1617  return QTextBlock(p, p->blockMap().next(n));
1618 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
uint next(uint n) const
bool isValid() const
Returns true if this text block is valid; otherwise returns false.
Definition: qtextobject.h:208
bool isValid(uint n) const

◆ operator!=()

bool QTextBlock::operator!= ( const QTextBlock o) const
inline

Returns true if this text block is different from the other text block.

Definition at line 211 of file qtextobject.h.

211 { return p != o.p || n != o.n; }
QTextDocumentPrivate * p
Definition: qtextobject.h:287

◆ operator<()

bool QTextBlock::operator< ( const QTextBlock o) const
inline

Returns true if this text block occurs before the other text block in the document.

Definition at line 212 of file qtextobject.h.

212 { return position() < o.position(); }
int position() const
Returns the index of the block&#39;s first character within the document.

◆ operator=()

QTextBlock & QTextBlock::operator= ( const QTextBlock o)
inline

Assigns the other text block to this text block.

Definition at line 206 of file qtextobject.h.

206 { p = o.p; n = o.n; return *this; }
QTextDocumentPrivate * p
Definition: qtextobject.h:287

◆ operator==()

bool QTextBlock::operator== ( const QTextBlock o) const
inline

Returns true if this text block is the same as the other text block.

Definition at line 210 of file qtextobject.h.

210 { return p == o.p && n == o.n; }
QTextDocumentPrivate * p
Definition: qtextobject.h:287

◆ position()

int QTextBlock::position ( ) const

Returns the index of the block's first character within the document.

Definition at line 1098 of file qtextobject.cpp.

Referenced by QTextControlPrivate::activateLinkUnderCursor(), QTextCopyHelper::appendFragment(), QSyntaxHighlighterPrivate::applyFormatChanges(), QTextCursor::atBlockEnd(), QTextCursor::atBlockStart(), QTextDocumentLayout::blockBoundingRect(), QAccessibleTextWidget::characterRect(), QTextCursor::charFormat(), QTextCursor::columnNumber(), QTextCopyHelper::copy(), currentTextLine(), QTextLine::draw(), QTextDocumentLayoutPrivate::drawBlock(), QTextDocumentLayoutPrivate::drawFlow(), QTextDocumentLayoutPrivate::drawFrame(), QTextHtmlExporter::emitBlock(), QScriptDebuggerCodeView::event(), QTextControlPrivate::extendWordwiseSelection(), QScriptEdit::extraAreaMouseEvent(), QTextDocument::find(), findInBlock(), QTextControl::findNextPrevAnchor(), QTextEngine::formatIndex(), QPlainTextEditControl::hitTest(), QTextDocumentLayoutPrivate::hitTest(), QTextControlPrivate::inputMethodEvent(), QTextControl::inputMethodQuery(), isEmptyBlockAfterTable(), isEmptyBlockBeforeTable(), isLineSeparatorBlockAfterTable(), QTextEngine::itemize(), QTextControlPrivate::keyPressEvent(), QTextLine::layout_helper(), QTextDocumentLayoutPrivate::layoutFlow(), QTextDocumentPrivate::leftCursorPosition(), QTextBlockGroupPrivate::markBlocksDirty(), QTextCursorPrivate::movePosition(), QTextCursor::movePosition(), QTextDocumentPrivate::nextCursorPosition(), operator<(), QPlainTextEdit::paintEvent(), QTextCursor::positionInBlock(), QTextDocumentLayout::positionInlineObject(), QTextDocumentPrivate::previousCursorPosition(), QTextCursor::QTextCursor(), QTextControlPrivate::rectForPosition(), QSyntaxHighlighterPrivate::reformatBlocks(), QTextDocumentPrivate::rightCursorPosition(), QTextControl::selectionRect(), setBlockCharFormatHelper(), QTextDocumentPrivate::setBlockFormat(), QTextEdit::setText(), QTextCursorPrivate::setX(), QTextEngine::shape(), QTextDocumentPrivate::undoRedo(), and QTextOdfWriter::writeFrame().

1099 {
1100  if (!p || !n)
1101  return 0;
1102 
1103  return p->blockMap().position(n);
1104 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
uint position(uint node, uint field=0) const
const BlockMap & blockMap() const

◆ previous()

QTextBlock QTextBlock::previous ( ) const

Returns the text block in the document before this block, or an empty text block if this is the first one.

Note that the next block may be in a different frame or table to this block.

See also
next() begin() end()

Definition at line 1628 of file qtextobject.cpp.

Referenced by QPlainTextEditPrivate::_q_adjustScrollbars(), QPlainTextEditControl::blockBoundingRect(), QPlainTextEditPrivate::ensureVisible(), QTextDocument::find(), QTextControl::findNextPrevAnchor(), QPlainTextEditControl::hitTest(), QTextCursorPrivate::movePosition(), QTextCursor::movePosition(), QPlainTextEditPrivate::pageUpDown(), and qTextBlockPosition().

1629 {
1630  if (!p)
1631  return QTextBlock();
1632 
1633  return QTextBlock(p, p->blockMap().previous(n));
1634 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
uint previous(uint n) const

◆ revision()

int QTextBlock::revision ( ) const

Returns the blocks revision.

Since
4.4
See also
setRevision(), QTextDocument::revision()

Definition at line 1434 of file qtextobject.cpp.

1435 {
1436  if (!p || !n)
1437  return -1;
1438 
1439  const QTextBlockData *b = p->blockMap().fragment(n);
1440  return b->revision;
1441 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
Fragment * fragment(uint index)

◆ setLineCount()

void QTextBlock::setLineCount ( int  count)
Since
4.5

Sets the line count to count.

See also
lineCount()

Definition at line 1546 of file qtextobject.cpp.

Referenced by QPlainTextDocumentLayoutPrivate::relayout().

1547 {
1548  if (!p || !n)
1549  return;
1550  p->blockMap().setSize(n, count, 2);
1551 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
void setSize(uint node, int new_size, uint field=0)
const BlockMap & blockMap() const

◆ setRevision()

void QTextBlock::setRevision ( int  rev)

Sets a blocks revision to rev.

Since
4.4
See also
revision(), QTextDocument::revision()

Definition at line 1453 of file qtextobject.cpp.

1454 {
1455  if (!p || !n)
1456  return;
1457 
1458  const QTextBlockData *b = p->blockMap().fragment(n);
1459  b->revision = rev;
1460 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
Fragment * fragment(uint index)

◆ setUserData()

void QTextBlock::setUserData ( QTextBlockUserData data)

Attaches the given data object to the text block.

Since
4.1

QTextBlockUserData can be used to store custom settings. The ownership is passed to the underlying text document, i.e. the provided QTextBlockUserData object will be deleted if the corresponding text block gets deleted. The user data object is not stored in the undo history, so it will not be available after undoing the deletion of a text block.

For example, if you write a programming editor in an IDE, you may want to let your user set breakpoints visually in your code for an integrated debugger. In a programming editor a line of text usually corresponds to one QTextBlock. The QTextBlockUserData interface allows the developer to store data for each QTextBlock, like for example in which lines of the source code the user has a breakpoint set. Of course this could also be stored externally, but by storing it inside the QTextDocument, it will for example be automatically deleted when the user deletes the associated line. It's really just a way to store custom information in the QTextDocument without using custom properties in QTextFormat which would affect the undo/redo stack.

Definition at line 1378 of file qtextobject.cpp.

1379 {
1380  if (!p || !n)
1381  return;
1382 
1383  const QTextBlockData *b = p->blockMap().fragment(n);
1384  if (data != b->userData)
1385  delete b->userData;
1386  b->userData = data;
1387 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
static const char * data(const QByteArray &arr)
const BlockMap & blockMap() const
Fragment * fragment(uint index)
QTextBlockUserData * userData

◆ setUserState()

void QTextBlock::setUserState ( int  state)

Stores the specified state integer value in the text block.

Since
4.1

This may be useful for example in a syntax highlighter to store a text parsing state.

Definition at line 1415 of file qtextobject.cpp.

Referenced by QTextCopyHelper::appendFragment(), and QTextHtmlImporter::processBlockNode().

1416 {
1417  if (!p || !n)
1418  return;
1419 
1420  const QTextBlockData *b = p->blockMap().fragment(n);
1421  b->userState = state;
1422 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
Fragment * fragment(uint index)

◆ setVisible()

void QTextBlock::setVisible ( bool  visible)

Sets the block's visibility to visible.

Since
4.4
See also
isVisible()

Definition at line 1491 of file qtextobject.cpp.

1492 {
1493  if (!p || !n)
1494  return;
1495 
1496  const QTextBlockData *b = p->blockMap().fragment(n);
1497  b->hidden = !visible;
1498 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
Fragment * fragment(uint index)

◆ text()

QString QTextBlock::text ( ) const

Returns the block's contents as plain text.

See also
length() charFormat() blockFormat()

Definition at line 1288 of file qtextobject.cpp.

Referenced by QScriptDebuggerCodeView::event(), findInBlock(), QTextControl::inputMethodQuery(), isLineSeparatorBlockAfterTable(), QTextCursorPrivate::movePosition(), QSyntaxHighlighterPrivate::reformatBlock(), QAccessibleTextEdit::text(), and QTextEngine::validate().

1289 {
1290  if (!p || !n)
1291  return QString();
1292 
1293  const QString buffer = p->buffer();
1294  QString text;
1295  text.reserve(length());
1296 
1297  const int pos = position();
1299  QTextDocumentPrivate::FragmentIterator end = p->find(pos + length() - 1); // -1 to omit the block separator char
1300  for (; it != end; ++it) {
1301  const QTextFragmentData * const frag = it.value();
1302  text += QString::fromRawData(buffer.constData() + frag->stringPosition, frag->size_array[0]);
1303  }
1304 
1305  return text;
1306 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
QString text() const
Returns the block&#39;s contents as plain text.
#define it(className, varName)
iterator end() const
Returns a text block iterator pointing to the end of the text block.
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString fromRawData(const QChar *, int size)
Constructs a QString that uses the first size Unicode characters in the array unicode.
Definition: qstring.cpp:7673
FragmentMap::ConstIterator FragmentIterator
int position() const
Returns the index of the block&#39;s first character within the document.
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
FragmentIterator find(int pos) const
int length() const
Returns the length of the block in characters.
QString buffer() const
quint32 size_array[N]
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ textDirection()

Qt::LayoutDirection QTextBlock::textDirection ( ) const

Returns the resolved text direction.

Since
4.7

If the block has no explicit direction set, it will resolve the direction from the blocks content. Returns either Qt::LeftToRight or Qt::RightToLeft.

See also
QTextFormat::layoutDirection(), QString::isRightToLeft(), Qt::LayoutDirection

Definition at line 1247 of file qtextobject.cpp.

Referenced by QTextDocumentLayoutPrivate::drawListItem(), QTextHtmlExporter::emitBlockAttributes(), QTextList::itemText(), QTextDocumentLayoutPrivate::layoutBlock(), and QTextCursorPrivate::movePosition().

1248 {
1250  if (dir != Qt::LayoutDirectionAuto)
1251  return dir;
1252 
1254  if (dir != Qt::LayoutDirectionAuto)
1255  return dir;
1256 
1257  const QString buffer = p->buffer();
1258 
1259  const int pos = position();
1261  QTextDocumentPrivate::FragmentIterator end = p->find(pos + length() - 1); // -1 to omit the block separator char
1262  for (; it != end; ++it) {
1263  const QTextFragmentData * const frag = it.value();
1264  const QChar *p = buffer.constData() + frag->stringPosition;
1265  const QChar * const end = p + frag->size_array[0];
1266  while (p < end) {
1267  switch(QChar::direction(p->unicode()))
1268  {
1269  case QChar::DirL:
1270  return Qt::LeftToRight;
1271  case QChar::DirR:
1272  case QChar::DirAL:
1273  return Qt::RightToLeft;
1274  default:
1275  break;
1276  }
1277  ++p;
1278  }
1279  }
1280  return Qt::LeftToRight;
1281 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
#define it(className, varName)
iterator end() const
Returns a text block iterator pointing to the end of the text block.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QTextOption defaultTextOption
FragmentMap::ConstIterator FragmentIterator
Qt::LayoutDirection layoutDirection() const
Returns the document&#39;s layout direction.
Definition: qtextformat.h:340
int position() const
Returns the index of the block&#39;s first character within the document.
FragmentIterator find(int pos) const
LayoutDirection
Definition: qnamespace.h:1580
int length() const
Returns the length of the block in characters.
QString buffer() const
Qt::LayoutDirection textDirection() const
Returns the direction of the text layout defined by the option.
Definition: qtextoption.h:100
quint32 size_array[N]
Direction direction() const
Returns the character&#39;s direction.
Definition: qchar.cpp:889
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ textList()

QTextList * QTextBlock::textList ( ) const

If the block represents a list item, returns the list that the item belongs to; otherwise returns 0.

Definition at line 1322 of file qtextobject.cpp.

Referenced by QTextCopyHelper::appendFragment(), QTextHtmlExporter::emitBlock(), and QTextOdfWriter::writeBlock().

1323 {
1324  if (!isValid())
1325  return 0;
1326 
1327  const QTextBlockFormat fmt = blockFormat();
1328  QTextObject *obj = p->document()->objectForFormat(fmt);
1329  return qobject_cast<QTextList *>(obj);
1330 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
T qobject_cast(QObject *object)
Definition: qobject.h:375
QTextObject * objectForFormat(const QTextFormat &) const
Returns the text object associated with the format f.
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument...
Definition: qtextformat.h:545
bool isValid() const
Returns true if this text block is valid; otherwise returns false.
Definition: qtextobject.h:208
The QTextList class provides a decorated list of items in a QTextDocument.
Definition: qtextlist.h:57
The QTextObject class is a base class for different kinds of objects that can group parts of a QTextD...
Definition: qtextobject.h:64
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.
QTextDocument * document()

◆ userData()

QTextBlockUserData * QTextBlock::userData ( ) const

Returns a pointer to a QTextBlockUserData object if previously set with setUserData() or a null pointer.

Since
4.1

Definition at line 1341 of file qtextobject.cpp.

1342 {
1343  if (!p || !n)
1344  return 0;
1345 
1346  const QTextBlockData *b = p->blockMap().fragment(n);
1347  return b->userData;
1348 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
Fragment * fragment(uint index)
QTextBlockUserData * userData

◆ userState()

int QTextBlock::userState ( ) const

Returns the integer value previously set with setUserState() or -1.

Since
4.1

Definition at line 1397 of file qtextobject.cpp.

Referenced by QTextCopyHelper::appendFragment(), QTextHtmlExporter::emitBlockAttributes(), QSyntaxHighlighter::previousBlockState(), and QSyntaxHighlighterPrivate::reformatBlocks().

1398 {
1399  if (!p || !n)
1400  return -1;
1401 
1402  const QTextBlockData *b = p->blockMap().fragment(n);
1403  return b->userState;
1404 }
QTextDocumentPrivate * p
Definition: qtextobject.h:287
const BlockMap & blockMap() const
Fragment * fragment(uint index)

Friends and Related Functions

◆ QSyntaxHighlighter

friend class QSyntaxHighlighter
friend

Definition at line 201 of file qtextobject.h.

◆ QTextDocumentPrivate

friend class QTextDocumentPrivate
friend

Definition at line 289 of file qtextobject.h.

◆ QTextLayout

friend class QTextLayout
friend

Definition at line 290 of file qtextobject.h.

Properties

◆ n

int QTextBlock::n
private

◆ p

QTextDocumentPrivate* QTextBlock::p
private

The documentation for this class was generated from the following files: