Class: QgsTextFragment¶
Stores a fragment of document along with formatting overrides to be used when rendering the fragment.
Text fragments consist of either a block of text or another atomic component of a document (such as an image).
Each fragment has an associated characterFormat(), which
specifies the text formatting overrides to use when rendering the
fragment. Additionally, the characterFormat() may contain
properties for other fragment types, such as image paths and sizes for
image fragments.
Warning
This API is not considered stable and may change in future QGIS versions.
Added in version 3.14.
Methods
Applies a capitalization style to the fragment's text.  | 
|
Returns the character formatting for the fragment.  | 
|
Returns the horizontal advance associated with this fragment, when rendered using the specified base font within the specified render context.  | 
|
Returns   | 
|
Returns   | 
|
Returns   | 
|
Sets the character format for the fragment.  | 
|
Sets the text content of the fragment.  | 
|
Returns the text content of the fragment.  | 
- class qgis.core.QgsTextFragment[source]¶
 Bases:
object- __init__(text: str | None = '', format: QgsTextCharacterFormat = QgsTextCharacterFormat())
 Constructor for QgsTextFragment, with the specified
textand optional characterformat.- Parameters:
 text (Optional[str] = '')
format (
QgsTextCharacterFormat= QgsTextCharacterFormat())
- __init__(fragment: QTextFragment)
 Constructor for QgsTextFragment, based on the specified QTextFragment
fragment.- Parameters:
 fragment (QTextFragment)
- __init__(a0: QgsTextFragment)
 - Parameters:
 a0 (QgsTextFragment)
- applyCapitalization(self, capitalization: Qgis.Capitalization)[source]¶
 Applies a
capitalizationstyle to the fragment’s text.Added in version 3.16.
- Parameters:
 capitalization (Qgis.Capitalization)
- characterFormat(self) QgsTextCharacterFormat¶
 Returns the character formatting for the fragment.
See also
- Return type:
 
- horizontalAdvance(self, font: QFont, context: QgsRenderContext, fontHasBeenUpdatedForFragment: bool = False, scaleFactor: float = 1) float[source]¶
 Returns the horizontal advance associated with this fragment, when rendered using the specified base
fontwithin the specified rendercontext.Set
fontHasBeenUpdatedForFragmenttoTrueiffontalready represents the character format for this fragment.The optional
scaleFactorparameter can specify a font size scaling factor. It is recommended to set this toQgsTextRenderer.calculateScaleFactorForFormat()and then manually calculations based on the resultant font metrics. Failure to do so will result in poor quality text rendering at small font sizes.- Parameters:
 font (QFont)
context (QgsRenderContext)
fontHasBeenUpdatedForFragment (bool = False)
scaleFactor (float = 1)
- Return type:
 float
- isImage(self) bool[source]¶
 Returns
Trueif the fragment represents an image.Added in version 3.40.
- Return type:
 bool
- isTab(self) bool[source]¶
 Returns
Trueif the fragment consists of just a tab character.Added in version 3.38.
- Return type:
 bool
- isWhitespace(self) bool[source]¶
 Returns
Trueif the fragment consists of just whitespace characters, and does not contain any content to render.Added in version 3.38.
- Return type:
 bool
- setCharacterFormat(self, format: QgsTextCharacterFormat)[source]¶
 Sets the character
formatfor the fragment.See also
- Parameters:
 format (QgsTextCharacterFormat)