PySide6.QtGui.QTextBlock¶
- class QTextBlock¶
- The - QTextBlockclass provides a container for text fragments in a- QTextDocument. More_…- Synopsis¶- Methods¶- def - __init__()
- def - __iter__()
- def - begin()
- def - blockFormat()
- def - blockNumber()
- def - charFormat()
- def - clearLayout()
- def - contains()
- def - document()
- def - end()
- def - fragmentIndex()
- def - isValid()
- def - isVisible()
- def - layout()
- def - length()
- def - lineCount()
- def - next()
- def - __ne__()
- def - __lt__()
- def - __eq__()
- def - position()
- def - previous()
- def - revision()
- def - setLineCount()
- def - setRevision()
- def - setUserData()
- def - setUserState()
- def - setVisible()
- def - text()
- def - textDirection()
- def - textFormats()
- def - textList()
- def - userData()
- def - userState()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- A text block encapsulates a block or paragraph of text in a - QTextDocument.- QTextBlockprovides 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 - QTextCursorinstead.- 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().- QTextBlockprovides comparison operators to make it easier to work with blocks:- operator==()compares two block for equality,- operator!=()compares two blocks for inequality, and- operatordetermines whether a block precedes another in the same document.  - See also - __init__()¶
 - __init__(o)
- Parameters:
- o – - QTextBlock
 
 - Copies the - othertext block’s attributes to this text block.- __iter__()¶
- Return type:
- object 
 
 - Returns a text block iterator pointing to the beginning of the text block. - See also - blockFormat()¶
- Return type:
 
 - Returns the - QTextBlockFormatthat describes block-specific properties.- See also - blockFormatIndex()¶
- Return type:
- int 
 
 - Returns an index into the document’s internal list of block formats for the text block’s format. - See also - blockNumber()¶
- Return type:
- int 
 
 - Returns the number of this block, or -1 if the block is invalid. - See also - charFormat()¶
- Return type:
 
 - Returns the - QTextCharFormatthat describes the block’s character format. The block’s character format is used when inserting text into an empty block.- See also - charFormatIndex()¶
- Return type:
- int 
 
 - Returns an index into the document’s internal list of character formats for the text block’s character format. - See also - clearLayout()¶
 - Clears the - QTextLayoutthat is used to lay out and display the block’s contents.- See also - contains(position)¶
- Parameters:
- position – int 
- Return type:
- bool 
 
 - Returns - trueif the given- positionis located within the text block; otherwise returns- false.- document()¶
- Return type:
 
 - Returns the text document this text block belongs to, or - Noneif the text block does not belong to any document.- Returns a text block iterator pointing to the end of the text block. - See also - firstLineNumber()¶
- Return type:
- int 
 
 - 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. - See also - fragmentIndex()¶
- Return type:
- int 
 
 - isValid()¶
- Return type:
- bool 
 
 - Returns - trueif this text block is valid; otherwise returns- false.- isVisible()¶
- Return type:
- bool 
 
 - Returns - trueif the block is visible; otherwise returns- false.- See also - layout()¶
- Return type:
 
 - Returns the - QTextLayoutthat is used to lay out and display the block’s contents.- Note that the returned - QTextLayoutobject can only be modified from the documentChanged implementation of a- QAbstractTextDocumentLayoutsubclass. Any changes applied from the outside cause undefined behavior.- See also - length()¶
- Return type:
- int 
 
 - Returns the length of the block in characters. - Note - The length returned includes all formatting characters, for example, newline. - See also - lineCount()¶
- Return type:
- int 
 
 - Returns the line count. Not all document layouts support this feature. - See also - next()¶
- Return type:
 
 - 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 - __ne__(o)¶
- Parameters:
- o – - QTextBlock
- Return type:
- bool 
 
 - Returns - trueif this text block is different from the- othertext block.- __lt__(o)¶
- Parameters:
- o – - QTextBlock
- Return type:
- bool 
 
 - Returns - trueif this text block occurs before the- othertext block in the document.- __eq__(o)¶
- Parameters:
- o – - QTextBlock
- Return type:
- bool 
 
 - Returns - trueif this text block is the same as the- othertext block.- position()¶
- Return type:
- int 
 
 - Returns the index of the block’s first character within the document. - previous()¶
- Return type:
 
 - 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. - revision()¶
- Return type:
- int 
 
 - Returns the blocks revision. - See also - setLineCount(count)¶
- Parameters:
- count – int 
 
 - Sets the line count to - count.- See also - setRevision(rev)¶
- Parameters:
- rev – int 
 
 - Sets a blocks revision to - rev.- See also - setUserData(data)¶
- Parameters:
- data – - QTextBlockUserData
 
 - Attaches the given - dataobject to the text block.- QTextBlockUserDatacan be used to store custom settings. The ownership is passed to the underlying text document, i.e. the provided- QTextBlockUserDataobject 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- QTextBlockUserDatainterface 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- QTextDocumentwithout using custom properties in- QTextFormatwhich would affect the undo/redo stack.- See also - setUserState(state)¶
- Parameters:
- state – int 
 
 - Stores the specified - stateinteger value in the text block. This may be useful for example in a syntax highlighter to store a text parsing state.- See also - setVisible(visible)¶
- Parameters:
- visible – bool 
 
 - Sets the block’s visibility to - visible.- See also - text()¶
- Return type:
- str 
 
 - Returns the block’s contents as plain text. - See also - textDirection()¶
- Return type:
 
 - 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 - layoutDirection()- LayoutDirection- textFormats()¶
- Return type:
- .list of QTextLayout.FormatRange 
 
 - 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 - If the block represents a list item, returns the list that the item belongs to; otherwise returns - None.- userData()¶
- Return type:
 
 - Returns a pointer to a - QTextBlockUserDataobject, if one has been set with- setUserData(), or- None.- See also - userState()¶
- Return type:
- int 
 
 - Returns the integer value previously set with - setUserState()or -1.- See also - class iterator¶
- The - iteratorclass provides an iterator for reading the contents of a- QTextBlock. More_…- Synopsis¶- Methods¶- def - __init__()
- def - __iter__()
- def - __next__()
- def - atEnd()
- def - fragment()
- def - __ne__()
- def - __iadd__()
- def - __isub__()
- def - __eq__()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - A block consists of a sequence of text fragments. This class provides a way to iterate over these, and read their contents. It does not provide a way to modify the internal structure or contents of the block. - An iterator can be constructed and used to access the fragments within a text block in the following way: - QTextBlock.iterator it for it in currentBlock: currentFragment = it.fragment() if currentFragment.isValid(): processFragment(currentFragment) - See also - __init__()¶
 - Constructs an iterator for this text block. - __iter__()¶
- Return type:
- object 
 
 - __next__()¶
- Return type:
- object 
 
 - atEnd()¶
- Return type:
- bool 
 
 - Returns - trueif the current item is the last item in the text block.- fragment()¶
- Return type:
 
 - Returns the text fragment the iterator currently points to. - Returns true if this iterator is different from the - otheriterator; otherwise returns- false.- The postfix ++ operator ( - i++) advances the iterator to the next item in the text block and returns an iterator to the old current item.- The postfix – operator ( - i--) makes the preceding item current and returns an iterator to the old current item.- Returns true if this iterator is the same as the - otheriterator; otherwise returns- false.