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

applyCapitalization

Applies a capitalization style to the fragment's text.

characterFormat

Returns the character formatting for the fragment.

horizontalAdvance

Returns the horizontal advance associated with this fragment, when rendered using the specified base font within the specified render context.

isImage

Returns True if the fragment represents an image.

isTab

Returns True if the fragment consists of just a tab character.

isWhitespace

Returns True if the fragment consists of just whitespace characters, and does not contain any content to render.

setCharacterFormat

Sets the character format for the fragment.

setText

Sets the text content of the fragment.

text

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 text and optional character format.

Parameters:
__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 capitalization style to the fragment’s text.

Added in version 3.16.

Parameters:

capitalization (Qgis.Capitalization)

characterFormat(self) QgsTextCharacterFormat

Returns the character formatting for the fragment.

Return type:

QgsTextCharacterFormat

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 font within the specified render context.

Set fontHasBeenUpdatedForFragment to True if font already represents the character format for this fragment.

The optional scaleFactor parameter can specify a font size scaling factor. It is recommended to set this to QgsTextRenderer.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 True if the fragment represents an image.

Added in version 3.40.

Return type:

bool

isTab(self) bool[source]

Returns True if the fragment consists of just a tab character.

Added in version 3.38.

Return type:

bool

isWhitespace(self) bool[source]

Returns True if 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 format for the fragment.

Parameters:

format (QgsTextCharacterFormat)

setText(self, text: str | None)[source]

Sets the text content of the fragment.

See also

text()

Parameters:

text (Optional[str])

text(self) str[source]

Returns the text content of the fragment.

See also

setText()

Return type:

str