Class: QgsTextFragment

class qgis.core.QgsTextFragment

Bases: sip.wrapper

Stores a fragment of text along with formatting overrides to be used when rendering the fragment.

Warning

This API is not considered stable and may change in future QGIS versions.

New in version 3.14.

QgsTextFragment(text: str = ‘’, format: QgsTextCharacterFormat = QgsTextCharacterFormat()) Constructor for QgsTextFragment, with the specified text and optional character format.

QgsTextFragment(fragment: QTextFragment) Constructor for QgsTextFragment, based on the specified QTextFragment fragment.

QgsTextFragment(QgsTextFragment)

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.

setCharacterFormat

Sets the character format for the fragment.

setText

Sets the text content of the fragment.

text

Returns the text content of the fragment.

applyCapitalization(self, capitalization: QgsStringUtils.Capitalization)

Applies a capitalization style to the fragment’s text.

New in version 3.16.

Parameters

capitalization (QgsStringUtils.Capitalization) –

characterFormat(self) QgsTextCharacterFormat

Returns the character formatting for the fragment.

Return type

QgsTextCharacterFormat

horizontalAdvance(self, font: QFont, fontHasBeenUpdatedForFragment: bool = False, scaleFactor: float = 1) float

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

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.FONT_WORKAROUND_SCALE 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) –

  • fontHasBeenUpdatedForFragment (bool = False) –

  • scaleFactor (float = 1) –

Return type

float

setCharacterFormat(self, format: QgsTextCharacterFormat)

Sets the character format for the fragment.

Parameters

format (QgsTextCharacterFormat) –

setText(self, text: str)

Sets the text content of the fragment.

See also

text()

Parameters

text (str) –

text(self) str

Returns the text content of the fragment.

See also

setText()

Return type

str