Class: QgsLayoutItemScaleBar

A layout item subclass for scale bars.

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayoutItemScaleBar

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

alignment

Returns the scalebar alignment.

alternateFillSymbol

Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).

applyDefaultRendererSettings

Applies any default settings relating to the specified renderer to the item.

applyDefaultSettings

Applies the default scalebar settings to the scale bar.

applyDefaultSize

Applies the default size to the scale bar (scale bar 1/5 of map item width)

boxContentSpace

Returns the spacing (margin) between the scalebar box and content in millimeters.

brush

Returns the primary brush for the scalebar.

brush2

Returns the secondary brush for the scalebar.

divisionLineSymbol

Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).

fillColor

Returns the color used for fills in the scalebar.

fillColor2

Returns the secondary color used for fills in the scalebar.

fillSymbol

Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).

font

Returns the font used for drawing text in the scalebar.

fontColor

Returns the color used for drawing text in the scalebar.

guessUnits

Attempts to guess the most reasonable unit choice for the scalebar, given the current linked map's scale.

height

Returns the scalebar height (in millimeters).

labelBarSpace

Returns the spacing (in millimeters) between labels and the scalebar.

labelHorizontalPlacement

Returns the horizontal placement of text labels.

labelVerticalPlacement

Returns the vertical placement of text labels.

lineCapStyle

Returns the cap style used for drawing lines in the scalebar.

lineColor

Returns the color used for lines in the scalebar.

lineJoinStyle

Returns the join style used for drawing lines in the scalebar.

lineSymbol

Returns the line symbol used to render the scalebar (only used for some scalebar types).

lineWidth

Returns the line width in millimeters for lines in the scalebar.

linkedMap

Returns the map item linked to the scalebar.

mapUnitsPerScaleBarUnit

Returns the number of map units per scale bar unit used by the scalebar.

maximumBarWidth

Returns the maximum width (in millimeters) for scale bar segments.

method

Returns the scale calculation method, which determines how the bar's scale will be calculated.

minimumBarWidth

Returns the minimum width (in millimeters) for scale bar segments.

numberOfSegments

Returns the number of segments included in the scalebar.

numberOfSegmentsLeft

Returns the number of segments included in the left part of the scalebar.

numberOfSubdivisions

Returns the number of subdivisions for segments included in the right part of the scalebar (only used for some scalebar types).

numericFormat

Returns the numeric format used for numbers in the scalebar.

pen

Returns the pen used for drawing outlines in the scalebar.

resizeToMinimumWidth

Resizes the scale bar to its minimum width, without changing the height.

segmentSizeMode

Returns the size mode for the scale bar segments.

setAlignment

Sets the scalebar alignment.

setAlternateFillSymbol

Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).

setBoxContentSpace

Sets the space (margin) between the scalebar box and content in millimeters.

setDivisionLineSymbol

Sets the line symbol used to render the scalebar divisions (only used for some scalebar types).

setFillColor

Sets the color used for fills in the scalebar.

setFillColor2

Sets the secondary color used for fills in the scalebar.

setFillSymbol

Sets the primary fill symbol used to render the scalebar (only used for some scalebar types).

setFont

Sets the font used for drawing text in the scalebar.

setFontColor

Sets the color used for drawing text in the scalebar.

setHeight

Sets the scalebar height (in millimeters).

setLabelBarSpace

Sets the spacing (in millimeters) between labels and the scalebar.

setLabelHorizontalPlacement

Sets the horizontal placement of text labels.

setLabelVerticalPlacement

Sets the vertical placement of text labels.

setLineCapStyle

Sets the cap style used when drawing the lines in the scalebar.

setLineColor

Sets the color used for lines in the scalebar.

setLineJoinStyle

Sets the join style used when drawing the lines in the scalebar

setLineSymbol

Sets the line symbol used to render the scalebar (only used for some scalebar types).

setLineWidth

Sets the line width in millimeters for lines in the scalebar.

setLinkedMap

Sets the map item linked to the scalebar.

setMapUnitsPerScaleBarUnit

Sets the number of map units per scale bar unit used by the scalebar.

setMaximumBarWidth

Sets the maximum width (in millimeters) for scale bar segments.

setMethod

Sets the scale calculation method, which determines how the bar's scale will be calculated.

setMinimumBarWidth

Sets the minimum width (in millimeters) for scale bar segments.

setNumberOfSegments

Sets the number of segments included in the scalebar.

setNumberOfSegmentsLeft

Sets the number of segments included in the left part of the scalebar.

setNumberOfSubdivisions

Sets the number of subdivisions for segments included in the right part of the scalebar (only used for some scalebar types).

setNumericFormat

Sets the numeric format used for numbers in the scalebar.

setSegmentSizeMode

Sets the size mode for scale bar segments.

setStyle

Sets the scale bar style by name.

setSubdivisionLineSymbol

Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types).

setSubdivisionsHeight

Sets the scalebar subdivisions height (in millimeters) for segments included in the right part of the scalebar (only used for some scalebar types).

setTextFormat

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

setUnitLabel

Sets the label for units.

setUnits

Sets the distance units used by the scalebar.

setUnitsPerSegment

Sets the number of scalebar units per segment.

style

Returns the scale bar style name.

subdivisionLineSymbol

Returns the line symbol used to render the scalebar subdivisions (only used for some scalebar types).

subdivisionsHeight

Returns the scalebar subdivisions height (in millimeters) for segments included in the right part of the scalebar (only used for some scalebar types).

textFormat

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

unitLabel

Returns the label for units.

units

Returns the distance units used by the scalebar.

unitsPerSegment

Returns the number of scalebar units per segment.

update

Adjusts the scale bar box size and updates the item.

Static Methods

create

Returns a new scale bar item for the specified layout.

class qgis.core.QgsLayoutItemScaleBar[source]

Bases: QgsLayoutItem

__init__(layout: QgsLayout | None)

Constructor for QgsLayoutItemScaleBar, with the specified parent layout.

Parameters:

layout (Optional[QgsLayout])

alignment(self) Qgis.ScaleBarAlignment[source]

Returns the scalebar alignment.

See also

setAlignment()

Return type:

Qgis.ScaleBarAlignment

alternateFillSymbol(self) QgsFillSymbol | None[source]

Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).

Ownership is not transferred.

See also

fillSymbol()

Added in version 3.14.

Return type:

Optional[QgsFillSymbol]

applyDefaultRendererSettings(self, renderer: QgsScaleBarRenderer | None) bool[source]

Applies any default settings relating to the specified renderer to the item.

Returns True if settings were applied.

Added in version 3.14.

Parameters:

renderer (Optional[QgsScaleBarRenderer])

Return type:

bool

applyDefaultSettings(self)[source]

Applies the default scalebar settings to the scale bar.

applyDefaultSize(self, units: Qgis.DistanceUnit = Qgis.DistanceUnit.Meters)[source]

Applies the default size to the scale bar (scale bar 1/5 of map item width)

Parameters:

units (Qgis.DistanceUnit = Qgis.DistanceUnit.Meters)

boxContentSpace(self) float[source]

Returns the spacing (margin) between the scalebar box and content in millimeters.

Return type:

float

brush(self) QBrush[source]

Returns the primary brush for the scalebar.

Return type:

QBrush

Returns:

QBrush used for filling the scalebar

See also

brush2()

See also

pen()

Deprecated since version 3.40: Use fillSymbol() instead.

brush2(self) QBrush[source]

Returns the secondary brush for the scalebar. This is used for alternating color style scalebars, such as single and double box styles.

Return type:

QBrush

Returns:

QBrush used for secondary color areas

See also

brush()

Deprecated since version 3.40: Use alternateFillSymbol() instead.

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

Returns a new scale bar item for the specified layout.

The caller takes responsibility for deleting the returned object.

Parameters:

layout (Optional[QgsLayout])

Return type:

Optional[QgsLayoutItemScaleBar]

divisionLineSymbol(self) QgsLineSymbol | None[source]

Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).

Ownership is not transferred.

See also

lineSymbol()

Added in version 3.14.

Return type:

Optional[QgsLineSymbol]

fillColor(self) QColor[source]

Returns the color used for fills in the scalebar.

See also

setFillColor()

See also

fillColor2()

Deprecated since version 3.40: Use fillSymbol() instead.

Return type:

QColor

fillColor2(self) QColor[source]

Returns the secondary color used for fills in the scalebar.

See also

setFillColor2()

See also

fillColor()

Deprecated since version 3.40: Use alternateFillSymbol() instead.

Return type:

QColor

fillSymbol(self) QgsFillSymbol | None[source]

Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).

Ownership is not transferred.

See also

setFillSymbol()

Added in version 3.14.

Return type:

Optional[QgsFillSymbol]

font(self) QFont[source]

Returns the font used for drawing text in the scalebar.

See also

setFont()

Deprecated since version 3.40: Use textFormat() instead.

Return type:

QFont

fontColor(self) QColor[source]

Returns the color used for drawing text in the scalebar.

See also

setFontColor()

See also

font()

Deprecated since version 3.40: Use textFormat() instead.

Return type:

QColor

guessUnits(self) Qgis.DistanceUnit[source]

Attempts to guess the most reasonable unit choice for the scalebar, given the current linked map’s scale.

This method also considers the linked map’s CRS, in order to determine if metric or imperial units are more appropriate.

Return type:

Qgis.DistanceUnit

height(self) float[source]

Returns the scalebar height (in millimeters).

See also

setHeight()

Return type:

float

labelBarSpace(self) float[source]

Returns the spacing (in millimeters) between labels and the scalebar.

Return type:

float

labelHorizontalPlacement(self) Qgis.ScaleBarDistanceLabelHorizontalPlacement[source]

Returns the horizontal placement of text labels.

Added in version 3.10.

Return type:

Qgis.ScaleBarDistanceLabelHorizontalPlacement

labelVerticalPlacement(self) Qgis.ScaleBarDistanceLabelVerticalPlacement[source]

Returns the vertical placement of text labels.

Added in version 3.10.

Return type:

Qgis.ScaleBarDistanceLabelVerticalPlacement

lineCapStyle(self) Qt.PenCapStyle[source]

Returns the cap style used for drawing lines in the scalebar.

Deprecated since version 3.40: Use lineSymbol() instead.

Return type:

Qt.PenCapStyle

lineColor(self) QColor[source]

Returns the color used for lines in the scalebar.

See also

setLineColor()

Deprecated since version 3.40: Use lineSymbol() instead.

Return type:

QColor

lineJoinStyle(self) Qt.PenJoinStyle[source]

Returns the join style used for drawing lines in the scalebar.

Deprecated since version 3.40: Use lineSymbol() instead.

Return type:

Qt.PenJoinStyle

lineSymbol(self) QgsLineSymbol | None[source]

Returns the line symbol used to render the scalebar (only used for some scalebar types).

Ownership is not transferred.

See also

setLineSymbol()

Added in version 3.14.

Return type:

Optional[QgsLineSymbol]

lineWidth(self) float[source]

Returns the line width in millimeters for lines in the scalebar.

See also

setLineWidth()

Deprecated since version 3.40: Use lineSymbol() instead.

Return type:

float

linkedMap(self) QgsLayoutItemMap | None[source]

Returns the map item linked to the scalebar.

See also

setLinkedMap()

Return type:

Optional[QgsLayoutItemMap]

mapUnitsPerScaleBarUnit(self) float[source]

Returns the number of map units per scale bar unit used by the scalebar.

Return type:

float

maximumBarWidth(self) float[source]

Returns the maximum width (in millimeters) for scale bar segments. This property is only effective if the segmentSizeMode() is set to SegmentSizeFitWidth.

Return type:

float

method(self) Qgis.ScaleCalculationMethod[source]

Returns the scale calculation method, which determines how the bar’s scale will be calculated.

See also

setMethod()

Added in version 3.40.

Return type:

Qgis.ScaleCalculationMethod

minimumBarWidth(self) float[source]

Returns the minimum width (in millimeters) for scale bar segments. This property is only effective if the segmentSizeMode() is set to SegmentSizeFitWidth.

Return type:

float

numberOfSegments(self) int[source]

Returns the number of segments included in the scalebar.

Return type:

int

numberOfSegmentsLeft(self) int[source]

Returns the number of segments included in the left part of the scalebar.

Return type:

int

numberOfSubdivisions(self) int[source]

Returns the number of subdivisions for segments included in the right part of the scalebar (only used for some scalebar types).

Note

The number of subdivisions represents the number of subdivision segments, not the number of subdivision lines. E.g. if the number is 1 then NO subdivision lines will be shown.

Added in version 3.14.

Return type:

int

numericFormat(self) QgsNumericFormat | None[source]

Returns the numeric format used for numbers in the scalebar.

Added in version 3.12.

Return type:

Optional[QgsNumericFormat]

pen(self) QPen[source]

Returns the pen used for drawing outlines in the scalebar.

See also

brush()

Deprecated since version 3.40: Use lineSymbol() instead.

Return type:

QPen

resizeToMinimumWidth(self)[source]

Resizes the scale bar to its minimum width, without changing the height.

segmentSizeMode(self) Qgis.ScaleBarSegmentSizeMode[source]

Returns the size mode for the scale bar segments.

Return type:

Qgis.ScaleBarSegmentSizeMode

setAlignment(self, alignment: Qgis.ScaleBarAlignment)[source]

Sets the scalebar alignment.

See also

alignment()

Parameters:

alignment (Qgis.ScaleBarAlignment)

setAlternateFillSymbol(self, symbol: QgsFillSymbol | None)[source]

Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types). Ownership of symbol is transferred to the scalebar.

See also

setFillSymbol()

Added in version 3.14.

Parameters:

symbol (Optional[QgsFillSymbol])

setBoxContentSpace(self, space: float)[source]

Sets the space (margin) between the scalebar box and content in millimeters.

Parameters:

space (float)

setDivisionLineSymbol(self, symbol: QgsLineSymbol | None)[source]

Sets the line symbol used to render the scalebar divisions (only used for some scalebar types). Ownership of symbol is transferred to the scalebar.

See also

setLineSymbol()

Added in version 3.14.

Parameters:

symbol (Optional[QgsLineSymbol])

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

Sets the color used for fills in the scalebar.

See also

fillColor()

See also

setFillColor2()

Deprecated since version 3.40: Use setFillSymbol() instead.

Parameters:

color (Union[QColor, Qt.GlobalColor])

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

Sets the secondary color used for fills in the scalebar.

See also

fillColor2()

See also

setFillColor2()

Deprecated since version 3.40: Use setAlternateFillSymbol() instead.

Parameters:

color (Union[QColor, Qt.GlobalColor])

setFillSymbol(self, symbol: QgsFillSymbol | None)[source]

Sets the primary fill symbol used to render the scalebar (only used for some scalebar types). Ownership of symbol is transferred to the scalebar.

See also

fillSymbol()

Added in version 3.14.

Parameters:

symbol (Optional[QgsFillSymbol])

setFont(self, font: QFont)[source]

Sets the font used for drawing text in the scalebar.

See also

font()

Deprecated since version 3.40: Use setTextFormat() instead.

Parameters:

font (QFont)

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

Sets the color used for drawing text in the scalebar.

See also

fontColor()

See also

setFont()

Deprecated since version 3.40: Use setTextFormat() instead.

Parameters:

color (Union[QColor, Qt.GlobalColor])

setHeight(self, height: float)[source]

Sets the scalebar height (in millimeters).

See also

height()

Parameters:

height (float)

setLabelBarSpace(self, space: float)[source]

Sets the spacing (in millimeters) between labels and the scalebar.

See also

labelBarSpace()

Parameters:

space (float)

setLabelHorizontalPlacement(self, placement: Qgis.ScaleBarDistanceLabelHorizontalPlacement)[source]

Sets the horizontal placement of text labels.

Added in version 3.10.

Parameters:

placement (Qgis.ScaleBarDistanceLabelHorizontalPlacement)

setLabelVerticalPlacement(self, placement: Qgis.ScaleBarDistanceLabelVerticalPlacement)[source]

Sets the vertical placement of text labels.

Added in version 3.10.

Parameters:

placement (Qgis.ScaleBarDistanceLabelVerticalPlacement)

setLineCapStyle(self, style: Qt.PenCapStyle)[source]

Sets the cap style used when drawing the lines in the scalebar.

See also

lineCapStyle()

Deprecated since version 3.40: Use setLineSymbol() instead.

Parameters:

style (Qt.PenCapStyle)

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

Sets the color used for lines in the scalebar.

See also

lineColor()

Deprecated since version 3.40: Use setLineSymbol() instead.

Parameters:

color (Union[QColor, Qt.GlobalColor])

setLineJoinStyle(self, style: Qt.PenJoinStyle)[source]

Sets the join style used when drawing the lines in the scalebar

See also

lineJoinStyle()

Deprecated since version 3.40: Use setLineSymbol() instead.

Parameters:

style (Qt.PenJoinStyle)

setLineSymbol(self, symbol: QgsLineSymbol | None)[source]

Sets the line symbol used to render the scalebar (only used for some scalebar types). Ownership of symbol is transferred to the scalebar.

See also

lineSymbol()

Added in version 3.14.

Parameters:

symbol (Optional[QgsLineSymbol])

setLineWidth(self, width: float)[source]

Sets the line width in millimeters for lines in the scalebar.

See also

lineWidth()

Deprecated since version 3.40: Use setLineSymbol() instead.

Parameters:

width (float)

setLinkedMap(self, map: QgsLayoutItemMap | None)[source]

Sets the map item linked to the scalebar.

See also

linkedMap()

Parameters:

map (Optional[QgsLayoutItemMap])

setMapUnitsPerScaleBarUnit(self, units: float)[source]

Sets the number of map units per scale bar unit used by the scalebar.

Parameters:

units (float)

setMaximumBarWidth(self, maxWidth: float)[source]

Sets the maximum width (in millimeters) for scale bar segments. This property is only effective if the segmentSizeMode() is set to SegmentSizeFitWidth.

Parameters:

maxWidth (float)

setMethod(self, method: Qgis.ScaleCalculationMethod)[source]

Sets the scale calculation method, which determines how the bar’s scale will be calculated.

See also

method()

Added in version 3.40.

Parameters:

method (Qgis.ScaleCalculationMethod)

setMinimumBarWidth(self, minWidth: float)[source]

Sets the minimum width (in millimeters) for scale bar segments. This property is only effective if the segmentSizeMode() is set to SegmentSizeFitWidth.

Parameters:

minWidth (float)

setNumberOfSegments(self, segments: int)[source]

Sets the number of segments included in the scalebar.

Parameters:

segments (int)

setNumberOfSegmentsLeft(self, segments: int)[source]

Sets the number of segments included in the left part of the scalebar.

Parameters:

segments (int)

setNumberOfSubdivisions(self, subdivisions: int)[source]

Sets the number of subdivisions for segments included in the right part of the scalebar (only used for some scalebar types).

Note

The number of subdivisions represents the number of subdivision segments, not the number of subdivision lines. E.g. if the number is 1 then NO subdivision lines will be shown.

Added in version 3.14.

Parameters:

subdivisions (int)

setNumericFormat(self, format: QgsNumericFormat | None)[source]

Sets the numeric format used for numbers in the scalebar.

Ownership of format is transferred to the scalebar.

See also

numericFormat()

Added in version 3.12.

Parameters:

format (Optional[QgsNumericFormat])

setSegmentSizeMode(self, mode: Qgis.ScaleBarSegmentSizeMode)[source]

Sets the size mode for scale bar segments.

Parameters:

mode (Qgis.ScaleBarSegmentSizeMode)

setStyle(self, name: str | None)[source]

Sets the scale bar style by name.

The name parameter gives the (untranslated) style name. Possibilities are: ‘Single Box’, ‘Double Box’, ‘Line Ticks Middle’, ‘Line Ticks Down’, ‘Line Ticks Up’, ‘Stepped Line’, ‘Hollow’, ‘Numeric’.

See also

style()

Parameters:

name (Optional[str])

setSubdivisionLineSymbol(self, symbol: QgsLineSymbol | None)[source]

Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types). Ownership of symbol is transferred to the scalebar.

See also

setLineSymbol()

Added in version 3.14.

Parameters:

symbol (Optional[QgsLineSymbol])

setSubdivisionsHeight(self, height: float)[source]

Sets the scalebar subdivisions height (in millimeters) for segments included in the right part of the scalebar (only used for some scalebar types).

Added in version 3.14.

Parameters:

height (float)

setTextFormat(self, format: QgsTextFormat)[source]

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

See also

textFormat()

Added in version 3.2.

Parameters:

format (QgsTextFormat)

setUnitLabel(self, label: str | None)[source]

Sets the label for units.

See also

unitLabel()

Parameters:

label (Optional[str])

setUnits(self, units: Qgis.DistanceUnit)[source]

Sets the distance units used by the scalebar.

See also

units()

Parameters:

units (Qgis.DistanceUnit)

setUnitsPerSegment(self, units: float)[source]

Sets the number of scalebar units per segment.

Parameters:

units (float)

style(self) str[source]

Returns the scale bar style name.

See also

setStyle()

Return type:

str

subdivisionLineSymbol(self) QgsLineSymbol | None[source]

Returns the line symbol used to render the scalebar subdivisions (only used for some scalebar types).

Ownership is not transferred.

See also

lineSymbol()

Added in version 3.14.

Return type:

Optional[QgsLineSymbol]

subdivisionsHeight(self) float[source]

Returns the scalebar subdivisions height (in millimeters) for segments included in the right part of the scalebar (only used for some scalebar types).

Added in version 3.14.

Return type:

float

textFormat(self) QgsTextFormat[source]

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

See also

setTextFormat()

Added in version 3.2.

Return type:

QgsTextFormat

unitLabel(self) str[source]

Returns the label for units.

See also

setUnitLabel()

Return type:

str

units(self) Qgis.DistanceUnit[source]

Returns the distance units used by the scalebar.

See also

setUnits()

Return type:

Qgis.DistanceUnit

unitsPerSegment(self) float[source]

Returns the number of scalebar units per segment.

Return type:

float

update(self)[source]

Adjusts the scale bar box size and updates the item.