Class: QgsLayoutItemLabel

A layout item subclass for text labels.

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayoutItemLabel

Base classes

QgsLayoutItem

Base class for graphical items within a QgsLayout.

QgsLayoutObject

A base class for objects which belong to a layout.

QObject

QgsExpressionContextGenerator

Abstract interface for generating an expression context.

QGraphicsRectItem

QAbstractGraphicsShapeItem

QGraphicsItem

QgsLayoutUndoObjectInterface

Interface for layout objects which support undo/redo commands.

Methods

adjustSizeToText

Resizes the item so that the label's text fits to the item.

convertToStaticText

Converts the label's text() to a static string, by evaluating any expressions included in the text and replacing them with their current values.

currentText

Returns the text as it appears on the label (with evaluated expressions and other dynamic content).

font

Returns the label's current font.

fontColor

Returns the label font color.

hAlign

Returns the horizontal alignment of the label.

marginX

Returns the horizontal margin between the edge of the frame and the label contents, in layout units.

marginY

Returns the vertical margin between the edge of the frame and the label contents, in layout units.

mode

Returns the label's current mode.

setFont

Sets the label's current font.

setFontColor

Sets the label font color.

setHAlign

Sets the horizontal alignment of the label.

setMargin

Sets the margin between the edge of the frame and the label contents.

setMarginX

Sets the horizontal margin between the edge of the frame and the label contents, in layout units.

setMarginY

Sets the vertical margin between the edge of the frame and the label contents, in layout units.

setMode

Sets the label's current mode, allowing the label to switch between font based and HTML based rendering.

setText

Sets the label's preset text.

setTextFormat

Sets the text format used for drawing text in the label.

setVAlign

Sets for the vertical alignment of the label.

sizeForText

Returns the required item size (in layout units) for the label's text to fill the item.

text

Returns the label's preset text.

textFormat

Returns the text format used for drawing text in the label.

vAlign

Returns for the vertical alignment of the label.

Static Methods

create

Returns a new label item for the specified layout.

Attributes

ModeFont

ModeHtml

class qgis.core.QgsLayoutItemLabel[source]

Bases: QgsLayoutItem

__init__(layout: QgsLayout | None)

Constructor for QgsLayoutItemLabel, with the specified parent layout.

Parameters:

layout (Optional[QgsLayout])

class Mode

Bases: int

ModeFont = 0
ModeHtml = 1
adjustSizeToText(self)[source]

Resizes the item so that the label’s text fits to the item. Keeps the top left point stationary.

See also

sizeForText()

adjustSizeToText(self, referencePoint: QgsLayoutItem.ReferencePoint)[source]

Resizes the item so that the label’s text fits to the item.

Keeps the specified reference point stationary.

See also

sizeForText()

Added in version 3.42.

Parameters:

referencePoint (QgsLayoutItem.ReferencePoint)

convertToStaticText(self)[source]

Converts the label’s text() to a static string, by evaluating any expressions included in the text and replacing them with their current values.

Added in version 3.20.

static create(layout: QgsLayout | None) QgsLayoutItemLabel | None[source]

Returns a new label item for the specified layout.

The caller takes responsibility for deleting the returned object.

Parameters:

layout (Optional[QgsLayout])

Return type:

Optional[QgsLayoutItemLabel]

currentText(self) str[source]

Returns the text as it appears on the label (with evaluated expressions and other dynamic content).

See also

text()

Return type:

str

font(self) QFont[source]

Returns the label’s current font.

See also

setFont()

Deprecated since version 3.40: Use textFormat() instead (since QGIS 3.24).

Return type:

QFont

fontColor(self) QColor[source]

Returns the label font color.

See also

setFontColor()

Deprecated since version 3.40: Use textFormat() instead (since QGIS 3.24).

Return type:

QColor

hAlign(self) Qt.AlignmentFlag[source]

Returns the horizontal alignment of the label.

See also

vAlign()

See also

setHAlign()

Return type:

Qt.AlignmentFlag

marginX(self) float[source]

Returns the horizontal margin between the edge of the frame and the label contents, in layout units.

See also

setMargin()

See also

marginY()

Return type:

float

marginY(self) float[source]

Returns the vertical margin between the edge of the frame and the label contents, in layout units.

See also

setMargin()

See also

marginX()

Return type:

float

mode(self) QgsLayoutItemLabel.Mode[source]

Returns the label’s current mode.

See also

setMode()

Return type:

QgsLayoutItemLabel.Mode

setFont(self, font: QFont)[source]

Sets the label’s current font.

See also

font()

Deprecated since version 3.40: Use setTextFormat() instead (since QGIS 3.24).

Parameters:

font (QFont)

setFontColor(self, color: QColor | Qt.GlobalColor)[source]

Sets the label font color.

See also

fontColor()

Deprecated since version 3.40: Use setTextFormat() instead (since QGIS 3.24).

Parameters:

color (Union[QColor, Qt.GlobalColor])

setHAlign(self, alignment: Qt.AlignmentFlag)[source]

Sets the horizontal alignment of the label.

See also

hAlign()

See also

setVAlign()

Parameters:

alignment (Qt.AlignmentFlag)

setMargin(self, margin: float)[source]

Sets the margin between the edge of the frame and the label contents. This method sets both the horizontal and vertical margins to the same value. The margins can be individually controlled using the setMarginX() and setMarginY() methods.

Margins are set using the current layout units.

See also

setMarginX()

See also

setMarginY()

Parameters:

margin (float)

setMarginX(self, margin: float)[source]

Sets the horizontal margin between the edge of the frame and the label contents, in layout units.

See also

setMargin()

See also

setMarginY()

Parameters:

margin (float)

setMarginY(self, margin: float)[source]

Sets the vertical margin between the edge of the frame and the label contents, in layout units.

See also

setMargin()

See also

setMarginX()

Parameters:

margin (float)

setMode(self, mode: QgsLayoutItemLabel.Mode)[source]

Sets the label’s current mode, allowing the label to switch between font based and HTML based rendering.

See also

mode()

Parameters:

mode (QgsLayoutItemLabel.Mode)

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

Sets the label’s preset text.

See also

text()

Parameters:

text (Optional[str])

setTextFormat(self, format: QgsTextFormat)[source]

Sets the text format used for drawing text in the label.

See also

textFormat()

Added in version 3.24.

Parameters:

format (QgsTextFormat)

setVAlign(self, alignment: Qt.AlignmentFlag)[source]

Sets for the vertical alignment of the label.

See also

vAlign()

See also

setHAlign()

Parameters:

alignment (Qt.AlignmentFlag)

sizeForText(self) QSizeF[source]

Returns the required item size (in layout units) for the label’s text to fill the item.

Return type:

QSizeF

text(self) str[source]

Returns the label’s preset text.

See also

currentText()

See also

setText()

Return type:

str

textFormat(self) QgsTextFormat[source]

Returns the text format used for drawing text in the label.

See also

setTextFormat()

Added in version 3.24.

Return type:

QgsTextFormat

vAlign(self) Qt.AlignmentFlag[source]

Returns for the vertical alignment of the label.

See also

setVAlign()

See also

hAlign()

Return type:

Qt.AlignmentFlag