QtBase  v6.3.1
Classes | Public Types | Public Member Functions | Friends | List of all members
QTextBlock Class Reference

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

#include <qtextobject.h>

Classes

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

Public Types

typedef iterator Iterator
 

Public Member Functions

 QTextBlock (QTextDocumentPrivate *priv, int b)
 
 QTextBlock ()
 
 QTextBlock (const QTextBlock &o)
 
QTextBlockoperator= (const QTextBlock &o)
 
bool isValid () const
 
bool operator== (const QTextBlock &o) const
 
bool operator!= (const QTextBlock &o) const
 
bool operator< (const QTextBlock &o) const
 
int position () const
 
int length () const
 
bool contains (int position) const
 
QTextLayoutlayout () const
 
void clearLayout ()
 
QTextBlockFormat blockFormat () const
 
int blockFormatIndex () const
 
QTextCharFormat charFormat () const
 
int charFormatIndex () const
 
Qt::LayoutDirection textDirection () const
 
QString text () const
 
QList< QTextLayout::FormatRangetextFormats () const
 
const QTextDocumentdocument () const
 
QTextListtextList () const
 
QTextBlockUserDatauserData () const
 
void setUserData (QTextBlockUserData *data)
 
int userState () const
 
void setUserState (int state)
 
int revision () const
 
void setRevision (int rev)
 
bool isVisible () const
 
void setVisible (bool visible)
 
int blockNumber () const
 
int firstLineNumber () const
 
void setLineCount (int count)
 
int lineCount () const
 
iterator begin () const
 
iterator end () const
 
QTextBlock next () const
 
QTextBlock previous () const
 
int fragmentIndex () const
 

Friends

class QSyntaxHighlighter
 
class QTextDocumentPrivate
 
class QTextLayout
 

Detailed Description

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

\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: \l operator==() compares two block for equality, \l operator!=() compares two blocks for inequality, and \l operator<() determines whether a block precedes another in the same document.

See also
QTextBlockFormat, QTextCharFormat, QTextFragment

Definition at line 192 of file qtextobject.h.

Member Typedef Documentation

◆ Iterator

Qt-style synonym for QTextBlock::iterator.

Definition at line 270 of file qtextobject.h.

Constructor & Destructor Documentation

◆ QTextBlock() [1/3]

QTextBlock::QTextBlock ( QTextDocumentPrivate priv,
int  b 
)
inline

Definition at line 196 of file qtextobject.h.

◆ QTextBlock() [2/3]

QTextBlock::QTextBlock ( )
inline

Definition at line 197 of file qtextobject.h.

◆ QTextBlock() [3/3]

QTextBlock::QTextBlock ( const QTextBlock other)
inline

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

Definition at line 198 of file qtextobject.h.

Member Function Documentation

◆ 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 1456 of file qtextobject.cpp.

Here is the call graph for this function:

◆ blockFormat()

QTextBlockFormat QTextBlock::blockFormat ( ) const

Returns the QTextBlockFormat that describes block-specific properties.

See also
charFormat()

Definition at line 1041 of file qtextobject.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 1055 of file qtextobject.cpp.

◆ blockNumber()

int QTextBlock::blockNumber ( ) const
Since
4.4
Returns the number of this block, or -1 if the block is invalid.

\sa QTextCursor::blockNumber()

Definition at line 1398 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ 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 1070 of file qtextobject.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 1084 of file qtextobject.cpp.

◆ clearLayout()

void QTextBlock::clearLayout ( )
Since
4.4 Clears the QTextLayout that is used to lay out and display the block's contents.
See also
layout()

Definition at line 1026 of file qtextobject.cpp.

Here is the call graph for this function:

◆ 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 988 of file qtextobject.cpp.

Here is the call graph for this function:

◆ document()

const QTextDocument * QTextBlock::document ( ) const

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

Definition at line 1226 of file qtextobject.cpp.

◆ 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 1474 of file qtextobject.cpp.

Here is the call graph for this function:

◆ firstLineNumber()

int QTextBlock::firstLineNumber ( ) const
Since
4.5
Returns the first line number of this block, or -1 if the block is invalid.
Unless the layout supports it, the line number is identical to the block number.

\sa QTextBlock::blockNumber()

Definition at line 1414 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ fragmentIndex()

int QTextBlock::fragmentIndex ( ) const
inline

Definition at line 278 of file qtextobject.h.

◆ isValid()

bool QTextBlock::isValid ( ) const

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

Definition at line 849 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ isVisible()

bool QTextBlock::isVisible ( ) const
Since
4.4

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

See also
setVisible()

Definition at line 1364 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ 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 1008 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ 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 976 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ 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 1442 of file qtextobject.cpp.

◆ 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 1495 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ operator!=()

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

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

Definition at line 204 of file qtextobject.h.

◆ operator<()

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

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

Definition at line 205 of file qtextobject.h.

Here is the call graph for this function:

◆ operator=()

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

Assigns the other text block to this text block.

Definition at line 199 of file qtextobject.h.

◆ operator==()

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

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

Definition at line 203 of file qtextobject.h.

◆ position()

int QTextBlock::position ( ) const

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

Definition at line 960 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ 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 previous block may be in a different frame or table to this block.

See also
next(), begin(), end()

Definition at line 1511 of file qtextobject.cpp.

Here is the caller graph for this function:

◆ revision()

int QTextBlock::revision ( ) const
Since
4.4

Returns the blocks revision.

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

Definition at line 1332 of file qtextobject.cpp.

◆ setLineCount()

void QTextBlock::setLineCount ( int  count)
Since
4.5

Sets the line count to count.

See also
lineCount()

Definition at line 1429 of file qtextobject.cpp.

◆ setRevision()

void QTextBlock::setRevision ( int  rev)
Since
4.4

Sets a blocks revision to rev.

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

Definition at line 1348 of file qtextobject.cpp.

◆ setUserData()

void QTextBlock::setUserData ( QTextBlockUserData data)
Since
4.1

Attaches the given data object to the text block.

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 1285 of file qtextobject.cpp.

◆ setUserState()

void QTextBlock::setUserState ( int  state)
Since
4.1

Stores the specified state integer value in the text block. This may be useful for example in a syntax highlighter to store a text parsing state.

Definition at line 1316 of file qtextobject.cpp.

◆ setVisible()

void QTextBlock::setVisible ( bool  visible)
Since
4.4

Sets the block's visibility to visible.

See also
isVisible()

Definition at line 1380 of file qtextobject.cpp.

◆ text()

QString QTextBlock::text ( ) const

Returns the block's contents as plain text.

See also
length(), charFormat(), blockFormat()

Definition at line 1151 of file qtextobject.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ textDirection()

Qt::LayoutDirection QTextBlock::textDirection ( ) const
Since
4.7

Returns the resolved text direction.

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 1103 of file qtextobject.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ textFormats()

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

Returns the block's text format options as a list of continuous ranges of QTextCharFormat. The range's character format is used when inserting text within the range boundaries.

See also
charFormat(), blockFormat()

Definition at line 1180 of file qtextobject.cpp.

◆ textList()

QTextList * QTextBlock::textList ( ) const

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

Definition at line 1235 of file qtextobject.cpp.

◆ userData()

QTextBlockUserData * QTextBlock::userData ( ) const
Since
4.1

Returns a pointer to a QTextBlockUserData object, if one has been set with setUserData(), or \nullptr.

Definition at line 1251 of file qtextobject.cpp.

◆ userState()

int QTextBlock::userState ( ) const
Since
4.1

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

Definition at line 1301 of file qtextobject.cpp.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ QSyntaxHighlighter

friend class QSyntaxHighlighter
friend

Definition at line 194 of file qtextobject.h.

◆ QTextDocumentPrivate

friend class QTextDocumentPrivate
friend

Definition at line 283 of file qtextobject.h.

◆ QTextLayout

friend class QTextLayout
friend

Definition at line 284 of file qtextobject.h.


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