Class: QgsLayoutItemLabel

class qgis.core.QgsLayoutItemLabel

Bases: QgsLayoutItem

A layout item subclass for text labels.

QgsLayoutItemLabel(layout: QgsLayout) Constructor for QgsLayoutItemLabel, with the specified parent layout.

Methods

adjustPointForReferencePosition

Adjusts the specified point at which a reference position of the item sits and returns the top left corner of the item, if reference point were placed at the specified position.

adjustSizeToText

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

applyDataDefinedSize

Applies any present data defined size overrides to the specified layout size.

applyItemSizeConstraint

Applies any item-specific size constraint handling to a given targetSize in layout units.

boundingRect

rtype:

QRectF

childEvent

connectNotify

contextMenuEvent

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.

create

Returns a new label item for the specified layout.

currentText

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

customEvent

disconnectNotify

displayName

rtype:

str

dragEnterEvent

dragLeaveEvent

dragMoveEvent

draw

param context:

drawBackground

Draws the background for the item.

drawDebugRect

Draws a debugging rectangle of the item's current bounds within the specified painter.

drawFrame

Draws the frame around the item.

drawRefreshingOverlay

Draws a "refreshing" overlay icon on the item.

dropEvent

focusInEvent

focusOutEvent

font

Returns the label's current font.

fontColor

Returns the label font color.

framePath

Returns the path to use when drawing the item's frame or background.

hAlign

Returns the horizontal alignment of the label.

hoverEnterEvent

hoverLeaveEvent

hoverMoveEvent

icon

rtype:

QIcon

inputMethodEvent

inputMethodQuery

isSignalConnected

itemChange

keyPressEvent

keyReleaseEvent

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.

mouseDoubleClickEvent

mouseMoveEvent

mousePressEvent

mouseReleaseEvent

positionAtReferencePoint

Returns the current position (in layout units) of a reference point for the item.

prepareGeometryChange

readObjectPropertiesFromElement

Sets object properties from a DOM element

readPropertiesFromElement

param element:

receivers

refresh

refreshBackgroundColor

Refresh item's background color, considering data defined colors.

refreshBlendMode

Refresh item's blend mode, considering data defined blend mode.

refreshFrame

Refresh item's frame, considering data defined colors and frame size.

refreshItemPosition

Refreshes an item's position by rechecking it against any possible overrides such as data defined positioning.

refreshItemRotation

Refreshes an item's rotation by rechecking it against any possible overrides such as data defined rotation.

refreshItemSize

Refreshes an item's size by rechecking it against any possible item fixed or minimum sizes.

refreshOpacity

Refresh item's opacity, considering data defined opacity.

sceneEvent

sceneEventFilter

sender

senderSignalIndex

setFixedSize

Sets a fixed size for the layout item, which prevents it from being freely resized.

setFont

Sets the label's current font.

setFontColor

Sets the label font color.

setFrameEnabled

param drawFrame:

setFrameStrokeWidth

param strokeWidth:

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.

setMinimumSize

Sets the minimum allowed size for the layout item.

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.

timerEvent

topLeftToReferencePoint

Returns the position for the reference point of the item, if the top-left of the item was placed at the specified point.

type

rtype:

int

updateMicroFocus

vAlign

Returns for the vertical alignment of the label.

wheelEvent

writeObjectPropertiesToElement

Stores object properties within an XML DOM element.

writePropertiesToElement

param element:

Attributes

ModeFont

ModeHtml

class Mode

Bases: int

ModeFont = 0
ModeHtml = 1
adjustPointForReferencePosition(self, point: QPointF | QPoint, size: QSizeF, reference: QgsLayoutItem.ReferencePoint) QPointF

Adjusts the specified point at which a reference position of the item sits and returns the top left corner of the item, if reference point were placed at the specified position.

adjustSizeToText(self)

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

See also

sizeForText()

applyDataDefinedSize(self, size: QgsLayoutSize) QgsLayoutSize

Applies any present data defined size overrides to the specified layout size.

applyItemSizeConstraint(self, targetSize: QSizeF) QSizeF

Applies any item-specific size constraint handling to a given targetSize in layout units. Subclasses can override this method if they need to apply advanced logic regarding item sizes, which cannot be covered by setFixedSize() or setMinimumSize(). Item size constraints are applied after fixed, minimum and data defined size constraints.

See also

setFixedSize()

See also

setMinimumSize()

boundingRect(self) QRectF
Return type:

QRectF

childEvent(self, QChildEvent)
connectNotify(self, QMetaMethod)
contextMenuEvent(self, QGraphicsSceneContextMenuEvent)
convertToStaticText(self)

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

New in version 3.20.

create(layout: QgsLayout) QgsLayoutItemLabel

Returns a new label item for the specified layout.

The caller takes responsibility for deleting the returned object.

Parameters:

layout (QgsLayout) –

Return type:

QgsLayoutItemLabel

currentText(self) str

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

See also

text()

Return type:

str

customEvent(self, QEvent)
disconnectNotify(self, QMetaMethod)
displayName(self) str
Return type:

str

dragEnterEvent(self, QGraphicsSceneDragDropEvent)
dragLeaveEvent(self, QGraphicsSceneDragDropEvent)
dragMoveEvent(self, QGraphicsSceneDragDropEvent)
draw(self, context: QgsLayoutItemRenderContext)
Parameters:

context (QgsLayoutItemRenderContext) –

drawBackground(self, context: QgsRenderContext)

Draws the background for the item.

See also

framePath()

drawDebugRect(self, painter: QPainter)

Draws a debugging rectangle of the item’s current bounds within the specified painter.

Parameters:

painter – destination QPainter

drawFrame(self, context: QgsRenderContext)

Draws the frame around the item.

See also

framePath()

drawRefreshingOverlay(self, painter: QPainter, itemStyle: QStyleOptionGraphicsItem)

Draws a “refreshing” overlay icon on the item.

New in version 3.32.

dropEvent(self, QGraphicsSceneDragDropEvent)
focusInEvent(self, QFocusEvent)
focusOutEvent(self, QFocusEvent)
font(self) QFont

Returns the label’s current font.

See also

setFont()

Deprecated since version use: textFormat() instead (since QGIS 3.24)

Return type:

QFont

fontColor(self) QColor

Returns the label font color.

See also

setFontColor()

Deprecated since version use: textFormat() instead (since QGIS 3.24)

Return type:

QColor

framePath(self) QPainterPath

Returns the path to use when drawing the item’s frame or background.

See also

drawFrame()

See also

drawBackground()

New in version 3.16.

hAlign(self) Qt.AlignmentFlag

Returns the horizontal alignment of the label.

See also

vAlign()

See also

setHAlign()

Return type:

Qt.AlignmentFlag

hoverEnterEvent(self, QGraphicsSceneHoverEvent)
hoverLeaveEvent(self, QGraphicsSceneHoverEvent)
hoverMoveEvent(self, QGraphicsSceneHoverEvent)
icon(self) QIcon
Return type:

QIcon

inputMethodEvent(self, QInputMethodEvent)
inputMethodQuery(self, Qt.InputMethodQuery) Any
isSignalConnected(self, QMetaMethod) bool
itemChange(self, QGraphicsItem.GraphicsItemChange, Any) Any
keyPressEvent(self, QKeyEvent)
keyReleaseEvent(self, QKeyEvent)
marginX(self) float

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

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

Returns the label’s current mode.

See also

setMode()

Return type:

QgsLayoutItemLabel.Mode

mouseDoubleClickEvent(self, QGraphicsSceneMouseEvent)
mouseMoveEvent(self, QGraphicsSceneMouseEvent)
mousePressEvent(self, QGraphicsSceneMouseEvent)
mouseReleaseEvent(self, QGraphicsSceneMouseEvent)
positionAtReferencePoint(self, reference: QgsLayoutItem.ReferencePoint) QPointF

Returns the current position (in layout units) of a reference point for the item.

prepareGeometryChange(self)
readObjectPropertiesFromElement(self, parentElement: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool

Sets object properties from a DOM element

Parameters:
  • parentElement – is the parent DOM element for the object

  • document – DOM document

  • context – read write context

Returns:

True if read was successful

readPropertiesFromElement(self, element: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool
Parameters:
Return type:

bool

receivers(self, PYQT_SIGNAL) int
refresh(self)
refreshBackgroundColor(self, updateItem: bool = True)

Refresh item’s background color, considering data defined colors. If updateItem is set to False, the item will not be automatically updated after the frame color is set and a later call to update() must be made.

refreshBlendMode(self)

Refresh item’s blend mode, considering data defined blend mode.

refreshFrame(self, updateItem: bool = True)

Refresh item’s frame, considering data defined colors and frame size. If updateItem is set to False, the item will not be automatically updated after the frame is set and a later call to update() must be made.

refreshItemPosition(self)

Refreshes an item’s position by rechecking it against any possible overrides such as data defined positioning.

refreshItemRotation(self, origin: QPointF | QPoint = None)

Refreshes an item’s rotation by rechecking it against any possible overrides such as data defined rotation.

The optional origin point specifies the origin (in item coordinates) around which the rotation should be applied.

refreshItemSize(self)

Refreshes an item’s size by rechecking it against any possible item fixed or minimum sizes.

See also

setFixedSize()

See also

setMinimumSize()

refreshOpacity(self, updateItem: bool = True)

Refresh item’s opacity, considering data defined opacity. If updateItem is set to False the item will not be automatically updated after the opacity is set and a later call to update() must be made.

sceneEvent(self, QEvent) bool
sceneEventFilter(self, QGraphicsItem, QEvent) bool
sender(self) QObject
senderSignalIndex(self) int
setFixedSize(self, size: QgsLayoutSize)

Sets a fixed size for the layout item, which prevents it from being freely resized. Set an empty size if item can be freely resized.

See also

fixedSize()

See also

setMinimumSize()

setFont(self, font: QFont)

Sets the label’s current font.

See also

font()

Deprecated since version use: setTextFormat() instead (since QGIS 3.24)

Parameters:

font (QFont) –

setFontColor(self, color: QColor | Qt.GlobalColor | QGradient)

Sets the label font color.

See also

fontColor()

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

Parameters:

color (Union[QColor) –

setFrameEnabled(self, drawFrame: bool)
Parameters:

drawFrame (bool) –

setFrameStrokeWidth(self, strokeWidth: QgsLayoutMeasurement)
Parameters:

strokeWidth (QgsLayoutMeasurement) –

setHAlign(self, alignment: Qt.AlignmentFlag)

Sets the horizontal alignment of the label.

See also

hAlign()

See also

setVAlign()

Parameters:

alignment (Qt.AlignmentFlag) –

setMargin(self, margin: float)

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)

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)

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

setMinimumSize(self, size: QgsLayoutSize)

Sets the minimum allowed size for the layout item. Set an empty size if item can be freely resized.

See also

minimumSize()

See also

setFixedSize()

setMode(self, mode: QgsLayoutItemLabel.Mode)

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)

Sets the label’s preset text.

See also

text()

Parameters:

text (str) –

setTextFormat(self, format: QgsTextFormat)

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

See also

textFormat()

New in version 3.24.

Parameters:

format (QgsTextFormat) –

setVAlign(self, alignment: Qt.AlignmentFlag)

Sets for the vertical alignment of the label.

See also

vAlign()

See also

setHAlign()

Parameters:

alignment (Qt.AlignmentFlag) –

sizeForText(self) QSizeF

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

Return type:

QSizeF

text(self) str

Returns the label’s preset text.

See also

currentText()

See also

setText()

Return type:

str

textFormat(self) QgsTextFormat

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

See also

setTextFormat()

New in version 3.24.

Return type:

QgsTextFormat

timerEvent(self, QTimerEvent)
topLeftToReferencePoint(self, point: QgsLayoutPoint) QgsLayoutPoint

Returns the position for the reference point of the item, if the top-left of the item was placed at the specified point.

type(self) int
Return type:

int

updateMicroFocus(self)
vAlign(self) Qt.AlignmentFlag

Returns for the vertical alignment of the label.

See also

setVAlign()

See also

hAlign()

Return type:

Qt.AlignmentFlag

wheelEvent(self, QGraphicsSceneWheelEvent)
writeObjectPropertiesToElement(self, parentElement: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool

Stores object properties within an XML DOM element.

Parameters:
  • parentElement – is the parent DOM element to store the object’s properties in

  • document – DOM document

  • context – read write context

Returns:

True if write was successful

writePropertiesToElement(self, element: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool
Parameters:
Return type:

bool