Class: QgsLayoutItemMapGrid

An individual grid which is drawn above the map content in a QgsLayoutItemMap.

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayoutItemMapGrid

Base classes

QgsLayoutItemMapItem

An item which is drawn inside a QgsLayoutItemMap, e.g., a grid or map overview.

QgsLayoutObject

A base class for objects which belong to a layout.

QObject

QgsExpressionContextGenerator

Abstract interface for generating an expression context.

class qgis.core.QgsLayoutItemMapGrid[source]

Bases: QgsLayoutItemMapItem

__init__(name: str | None, map: QgsLayoutItemMap | None)

Constructor for QgsLayoutItemMapGrid.

Parameters:
AboveTick = 4
class AnnotationCoordinate

Bases: int

class AnnotationDirection

Bases: int

class AnnotationFormat

Bases: int

class AnnotationPosition

Bases: int

class BorderSide

Bases: int

Bottom = 2
BoundaryDirection = 3
CM = 2
Cross = 1
CustomFormat = 8
Decimal = 0
DecimalWithSuffix = 3
DegreeMinute = 1
DegreeMinuteNoSuffix = 4
DegreeMinutePadded = 5
DegreeMinuteSecond = 2
DegreeMinuteSecondNoSuffix = 6
DegreeMinuteSecondPadded = 7
class DisplayMode

Bases: int

DynamicPageSizeBased = 3
ExteriorTicks = 3
FrameAnnotationsOnly = 3
FrameBottom = 8
FrameLeft = 1
FrameRight = 2
class FrameSideFlag

Bases: int

class FrameSideFlags
class FrameSideFlags(f: QgsLayoutItemMapGrid.FrameSideFlags | QgsLayoutItemMapGrid.FrameSideFlag)
class FrameSideFlags(a0: QgsLayoutItemMapGrid.FrameSideFlags)

Bases: object

class FrameStyle

Bases: int

FrameTop = 4
class GridStyle

Bases: int

class GridUnit

Bases: int

HideAll = 3
Horizontal = 0
InsideMapFrame = 0
InteriorExteriorTicks = 4
InteriorTicks = 2
Latitude = 1
LatitudeOnly = 1
Left = 0
LineBorder = 5
LineBorderNautical = 6
Longitude = 0
LongitudeOnly = 2
MM = 1
MapUnit = 0
Markers = 2
NoFrame = 0
NormalizedTicks = 1
OnTick = 5
OrthogonalTicks = 0
OutsideMapFrame = 1
Right = 1
ShowAll = 0
Solid = 0
class TickLengthMode

Bases: int

Top = 3
UnderTick = 6
Vertical = 1
VerticalDescending = 2
Zebra = 1
ZebraNautical = 7
annotationDirection(self, border: QgsLayoutItemMapGrid.BorderSide) QgsLayoutItemMapGrid.AnnotationDirection[source]

Returns the direction for drawing frame annotations, on the specified side of the map.

Parameters:

border (QgsLayoutItemMapGrid.BorderSide)

Return type:

QgsLayoutItemMapGrid.AnnotationDirection

annotationDisplay(self, border: QgsLayoutItemMapGrid.BorderSide) QgsLayoutItemMapGrid.DisplayMode[source]

Returns the display mode for the grid annotations on a specified side of the map frame. This property also specifies whether annotations have been disabled from a side of the map frame.

Parameters:

border (QgsLayoutItemMapGrid.BorderSide) – side of map for annotations

Return type:

QgsLayoutItemMapGrid.DisplayMode

Returns:

display mode for grid annotations

annotationEnabled(self) bool[source]

Returns whether annotations are shown for the grid.

Return type:

bool

annotationExpression(self) str[source]

Returns the expression used for drawing grid annotations. This is only used when annotationFormat() is QgsLayoutItemMapGrid.CustomFormat.

Return type:

str

annotationFont(self) QFont[source]

Returns the font used for drawing grid annotations. Shortcut for annotationTextFormat().font().

Deprecated since version 3.40: Use annotationTextFormat() instead.

Return type:

QFont

annotationFontColor(self) QColor[source]

Returns the font color used for drawing grid annotations. Shortcut for annotationTextFormat().color() and annotationTextFormat().opacity().

Deprecated since version 3.40: Use annotationTextFormat() instead.

Return type:

QColor

annotationFormat(self) QgsLayoutItemMapGrid.AnnotationFormat[source]

Returns the format for drawing grid annotations.

Return type:

QgsLayoutItemMapGrid.AnnotationFormat

annotationFrameDistance(self) float[source]

Returns the distance between the map frame and annotations. Units are in layout units.

Return type:

float

annotationPosition(self, side: QgsLayoutItemMapGrid.BorderSide) QgsLayoutItemMapGrid.AnnotationPosition[source]

Returns the position for the grid annotations on a specified side of the map frame.

Parameters:

side (QgsLayoutItemMapGrid.BorderSide)

Return type:

QgsLayoutItemMapGrid.AnnotationPosition

annotationPrecision(self) int[source]

Returns the coordinate precision for grid annotations, which is the number of decimal places shown when drawing grid annotations.

Return type:

int

annotationTextFormat(self) QgsTextFormat[source]

Returns the text format used when rendering grid annotations.

Added in version 3.16.

Return type:

QgsTextFormat

blendMode(self) QPainter.CompositionMode[source]

Retrieves the blending mode used for drawing the grid.

See also

setBlendMode()

Return type:

QPainter.CompositionMode

calculateMaxExtension(self)[source]

Calculates the maximum distance the grid extends beyond the QgsLayoutItemMap’s item rect. This method calculates the distance for each side of the map item separately.

See also

maxExtension()

copyProperties(self, other: QgsLayoutItemMapGrid | None)[source]

Copies properties from specified map grid.

Added in version 3.38.

Parameters:

other (Optional[QgsLayoutItemMapGrid])

crossLength(self) float[source]

Retrieves the length (in layout units) of the cross segments drawn for the grid. This is only used for grids with QgsLayoutItemMapGrid.Cross styles.

See also

setCrossLength()

Return type:

float

crs(self) QgsCoordinateReferenceSystem[source]

Retrieves the CRS for the grid.

See also

setCrs()

Return type:

QgsCoordinateReferenceSystem

signal crsChanged[source]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

frameDivisions(self, side: QgsLayoutItemMapGrid.BorderSide) QgsLayoutItemMapGrid.DisplayMode[source]

Returns the type of grid divisions which are used for frames on a specified side of the map.

Parameters:

side (QgsLayoutItemMapGrid.BorderSide)

Return type:

QgsLayoutItemMapGrid.DisplayMode

frameFillColor1(self) QColor[source]

Retrieves the first fill color for the grid frame.

See also

framePenColor()

Return type:

QColor

frameFillColor2(self) QColor[source]

Retrieves the second fill color for the grid frame.

See also

framePenColor()

Return type:

QColor

frameMargin(self) float[source]

Sets the grid frame Margin (in layout units). This property controls distance between the map frame and the grid frame.

See also

setFrameMargin()

Added in version 3.6.

Return type:

float

framePenColor(self) QColor[source]

Retrieves the color of the stroke drawn in the grid frame.

See also

framePenSize()

Return type:

QColor

framePenSize(self) float[source]

Retrieves the width of the stroke drawn in the grid frame.

See also

framePenColor()

Return type:

float

frameSideFlags(self) QgsLayoutItemMapGrid.FrameSideFlags[source]

Returns the flags which control which sides of the map item the grid frame is drawn on.

Return type:

QgsLayoutItemMapGrid.FrameSideFlags

frameStyle(self) QgsLayoutItemMapGrid.FrameStyle[source]

Returns the grid frame style.

See also

setFrameStyle()

Return type:

QgsLayoutItemMapGrid.FrameStyle

frameWidth(self) float[source]

Gets the grid frame width in layout units. This property controls how wide the grid frame is. The size of the line outlines drawn in the frame can be retrieved via the framePenSize method.

See also

setFrameWidth()

Return type:

float

intervalX(self) float[source]

Returns the interval between grid lines in the x-direction. The units are retrieved through the units() method.

See also

setIntervalX()

See also

intervalY()

Return type:

float

intervalY(self) float[source]

Returns the interval between grid lines in the y-direction. The units are retrieved through the units() method.

See also

setIntervalY()

See also

intervalX()

Return type:

float

lineSymbol(self) QgsLineSymbol | None[source]

Returns the line symbol used for drawing grid lines. This is only used for grids with QgsLayoutItemMapGrid.Solid or QgsLayoutItemMapGrid.Cross styles.

See also

setLineSymbol()

See also

markerSymbol()

See also

style()

Return type:

Optional[QgsLineSymbol]

markerSymbol(self) QgsMarkerSymbol | None[source]

Returns the marker symbol used for drawing grid points. This is only used for grids with a QgsLayoutItemMapGrid.Markers style.

See also

lineSymbol()

See also

style()

Return type:

Optional[QgsMarkerSymbol]

maxExtension(self) float[source]

Calculates the maximum distance the grid extends beyond the QgsLayoutItemMap’s item rect (in layout units).

Return type:

float

maximumIntervalWidth(self) float[source]

Returns the maximum width (in millimeters) for grid segments. This property is only effective if the units() is set to DynamicPageSizeBased.

See also

units()

Added in version 3.10.

Return type:

float

minimumIntervalWidth(self) float[source]

Returns the minimum width (in millimeters) for grid segments. This property is only effective if the units() is set to DynamicPageSizeBased.

See also

units()

Added in version 3.10.

Return type:

float

offsetX(self) float[source]

Returns the offset for grid lines in the x-direction. The units are retrieved through the units() method.

See also

setOffsetX()

See also

offsetY()

Return type:

float

offsetY(self) float[source]

Returns the offset for grid lines in the y-direction. The units are retrieved through the units() method.

See also

setOffsetY()

See also

offsetX()

Return type:

float

rotatedAnnotationsEnabled(self) bool[source]

Gets whether annotations rotation for rotated or reprojected grids is enabled.

Added in version 3.16.

Return type:

bool

rotatedAnnotationsLengthMode(self) QgsLayoutItemMapGrid.TickLengthMode[source]

Returns the annotation length calculation mode.

Added in version 3.16.

Return type:

QgsLayoutItemMapGrid.TickLengthMode

rotatedAnnotationsMarginToCorner(self) float[source]

Gets the margin to corners (in canvas units) below which outwards facing annotations are not drawn.

Added in version 3.16.

Return type:

float

rotatedAnnotationsMinimumAngle(self) float[source]

Gets the minimum angle (in degrees) below which annotations are not drawn.

Added in version 3.16.

Return type:

float

rotatedTicksEnabled(self) bool[source]

Gets whether ticks rotation for rotated or reprojected grids is enabled.

Added in version 3.16.

Return type:

bool

rotatedTicksLengthMode(self) QgsLayoutItemMapGrid.TickLengthMode[source]

Returns the grid frame style.

Added in version 3.16.

Return type:

QgsLayoutItemMapGrid.TickLengthMode

rotatedTicksMarginToCorner(self) float[source]

Gets the margin to corners (in canvas units) below which outwards facing ticks are not drawn.

Added in version 3.16.

Return type:

float

rotatedTicksMinimumAngle(self) float[source]

Gets the minimum angle (in degrees) below which ticks are not drawn.

Added in version 3.16.

Return type:

float

setAnnotationDirection(self, direction: QgsLayoutItemMapGrid.AnnotationDirection, side: QgsLayoutItemMapGrid.BorderSide)[source]

Sets the direction for drawing frame annotations for the specified map side.

setAnnotationDirection(self, direction: QgsLayoutItemMapGrid.AnnotationDirection) Sets the direction for drawing all frame annotations.

Parameters:
setAnnotationDisplay(self, display: QgsLayoutItemMapGrid.DisplayMode, border: QgsLayoutItemMapGrid.BorderSide)[source]

Sets what types of grid annotations should be drawn for a specified side of the map frame, or whether grid annotations should be disabled for the side.

Parameters:
setAnnotationEnabled(self, enabled: bool)[source]

Sets whether annotations should be shown for the grid.

Parameters:

enabled (bool)

setAnnotationExpression(self, expression: str | None)[source]

Sets the expression used for drawing grid annotations. This is only used when annotationFormat() is QgsLayoutItemMapGrid.CustomFormat.

Parameters:

expression (Optional[str])

setAnnotationFont(self, font: QFont)[source]

Sets the font used for drawing grid annotations. Shortcut for annotationTextFormat().setFont().

See also

annotationFont()

Deprecated since version 3.40: Use setAnnotationTextFormat() instead.

Parameters:

font (QFont)

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

Sets the font color used for drawing grid annotations. Shortcut for annotationTextFormat().setColor() and annotationTextFormat().setOpacity().

Deprecated since version 3.40: Use setAnnotationTextFormat() instead.

Parameters:

color (Union[QColor, Qt.GlobalColor])

setAnnotationFormat(self, format: QgsLayoutItemMapGrid.AnnotationFormat)[source]

Sets the format for drawing grid annotations.

Parameters:

format (QgsLayoutItemMapGrid.AnnotationFormat)

setAnnotationFrameDistance(self, distance: float)[source]

Sets the distance between the map frame and annotations. Units are layout units.

Parameters:

distance (float)

setAnnotationPosition(self, position: QgsLayoutItemMapGrid.AnnotationPosition, side: QgsLayoutItemMapGrid.BorderSide)[source]

Sets the position for the grid annotations on a specified side of the map frame.

Parameters:
setAnnotationPrecision(self, precision: int)[source]

Sets the coordinate precision for grid annotations. The precision indicates the number of decimal places to show when drawing grid annotations.

Parameters:

precision (int)

setAnnotationTextFormat(self, format: QgsTextFormat)[source]

Sets the text format to use when rendering grid annotations.

Added in version 3.16.

Parameters:

format (QgsTextFormat)

setBlendMode(self, mode: QPainter.CompositionMode)[source]

Sets the blending mode used for drawing the grid.

See also

blendMode()

Parameters:

mode (QPainter.CompositionMode)

setCrossLength(self, length: float)[source]

Sets the length (in layout units) of the cross segments drawn for the grid. This is only used for grids with QgsLayoutItemMapGrid.Cross styles.

See also

crossLength()

Parameters:

length (float)

setCrs(self, crs: QgsCoordinateReferenceSystem)[source]

Sets the crs for the grid.

See also

crs()

Parameters:

crs (QgsCoordinateReferenceSystem)

setFrameDivisions(self, divisions: QgsLayoutItemMapGrid.DisplayMode, side: QgsLayoutItemMapGrid.BorderSide)[source]

Sets what type of grid divisions should be used for frames on a specified side of the map.

See also

frameDivisions()

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

Sets the first fill color used for the grid frame.

Parameters:

color (Union[QColor, Qt.GlobalColor])

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

Sets the second fill color used for the grid frame.

Parameters:

color (Union[QColor, Qt.GlobalColor])

setFrameMargin(self, margin: float)[source]

Sets the grid frame margin (in layout units). This property controls distance between the map frame and the grid frame.

See also

frameMargin()

Added in version 3.6.

Parameters:

margin (float)

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

Sets the color of the stroke drawn in the grid frame.

See also

framePenColor()

Parameters:

color (Union[QColor, Qt.GlobalColor])

setFramePenSize(self, width: float)[source]

Sets the width of the stroke drawn in the grid frame.

See also

framePenSize()

Parameters:

width (float)

setFrameSideFlag(self, flag: QgsLayoutItemMapGrid.FrameSideFlag, on: bool = True)[source]

Sets whether the grid frame is drawn for a certain side of the map item.

Parameters:

See also

frameSideFlags()

setFrameSideFlags(self, flags: QgsLayoutItemMapGrid.FrameSideFlags | QgsLayoutItemMapGrid.FrameSideFlag)[source]

Sets flags for grid frame sides. Setting these flags controls which sides of the map item the grid frame is drawn on.

See also

frameSideFlags()

Parameters:

flags (Union[QgsLayoutItemMapGrid.FrameSideFlags, QgsLayoutItemMapGrid.FrameSideFlag])

setFrameStyle(self, style: QgsLayoutItemMapGrid.FrameStyle)[source]

Sets the grid frame style.

See also

frameStyle()

Parameters:

style (QgsLayoutItemMapGrid.FrameStyle)

setFrameWidth(self, width: float)[source]

Sets the grid frame width (in layout units). This property controls how wide the grid frame is. The size of the line outlines drawn in the frame is controlled through the setFramePenSize method.

See also

frameWidth()

Parameters:

width (float)

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

Sets the color of grid lines. This is only used for grids with QgsLayoutItemMapGrid.Solid or QgsLayoutItemMapGrid.Cross styles. For more control over grid line appearance, use setLineSymbol instead.

See also

setLineSymbol()

Parameters:

color (Union[QColor, Qt.GlobalColor])

setGridLineWidth(self, width: float)[source]

Sets the width of grid lines (in layout units). This is only used for grids with QgsLayoutItemMapGrid.Solid or QgsLayoutItemMapGrid.Cross styles. For more control over grid line appearance, use setLineSymbol instead.

See also

setLineSymbol()

Parameters:

width (float)

setIntervalX(self, interval: float)[source]

Sets the interval between grid lines in the x-direction. The units are controlled through the setUnits method

See also

setIntervalY()

See also

intervalX()

Parameters:

interval (float)

setIntervalY(self, interval: float)[source]

Sets the interval between grid lines in the y-direction. The units are controlled through the setUnits method

See also

setIntervalX()

See also

intervalY()

Parameters:

interval (float)

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

Sets the line symbol used for drawing grid lines. This is only used for grids with QgsLayoutItemMapGrid.Solid or QgsLayoutItemMapGrid.Cross styles. Ownership of symbol is transferred to the grid.

See also

lineSymbol()

See also

setStyle()

Parameters:

symbol (Optional[QgsLineSymbol])

setMarkerSymbol(self, symbol: QgsMarkerSymbol | None)[source]

Sets the marker symbol used for drawing grid points. This is only used for grids with a QgsLayoutItemMapGrid.Markers style. Ownership of symbol is transferred to the grid.

See also

markerSymbol()

See also

setLineSymbol()

See also

setStyle()

Parameters:

symbol (Optional[QgsMarkerSymbol])

setMaximumIntervalWidth(self, width: float)[source]

Sets the maximum width (in millimeters) for grid segments. This property is only effective if the units() is set to DynamicPageSizeBased.

See also

setUnits()

Added in version 3.10.

Parameters:

width (float)

setMinimumIntervalWidth(self, width: float)[source]

Sets the minimum width (in millimeters) for grid segments. This property is only effective if the units() is set to DynamicPageSizeBased.

See also

setUnits()

Added in version 3.10.

Parameters:

width (float)

setOffsetX(self, offset: float)[source]

Sets the offset for grid lines in the x-direction. The units are controlled through the setUnits method.

See also

setOffsetY()

See also

offsetX()

Parameters:

offset (float)

setOffsetY(self, offset: float)[source]

Sets the offset for grid lines in the y-direction. The units are controlled through the setUnits method.

See also

setOffsetX()

See also

offsetY()

Parameters:

offset (float)

setRotatedAnnotationsEnabled(self, state: bool)[source]

Enable/disable annotations rotation for rotated or reprojected grids.

Added in version 3.16.

Parameters:

state (bool)

setRotatedAnnotationsLengthMode(self, mode: QgsLayoutItemMapGrid.TickLengthMode)[source]

Sets the annotation length calculation mode.

Added in version 3.16.

Parameters:

mode (QgsLayoutItemMapGrid.TickLengthMode)

setRotatedAnnotationsMarginToCorner(self, margin: float)[source]

Sets the margin to corners (in canvas units) below which outwards facing annotations are not drawn.

Added in version 3.16.

Parameters:

margin (float)

setRotatedAnnotationsMinimumAngle(self, angle: float)[source]

Sets the minimum angle (in degrees) below which annotations are not drawn.

Added in version 3.16.

Parameters:

angle (float)

setRotatedTicksEnabled(self, state: bool)[source]

Enable/disable ticks rotation for rotated or reprojected grids.

Added in version 3.16.

Parameters:

state (bool)

setRotatedTicksLengthMode(self, mode: QgsLayoutItemMapGrid.TickLengthMode)[source]

Sets the tick length calculation mode.

Added in version 3.16.

Parameters:

mode (QgsLayoutItemMapGrid.TickLengthMode)

setRotatedTicksMarginToCorner(self, margin: float)[source]

Sets the margin to corners (in canvas units) below which outwards facing ticks are not drawn.

Added in version 3.16.

Parameters:

margin (float)

setRotatedTicksMinimumAngle(self, angle: float)[source]

Sets the minimum angle (in degrees) below which ticks are not drawn.

Added in version 3.16.

Parameters:

angle (float)

setStyle(self, style: QgsLayoutItemMapGrid.GridStyle)[source]

Sets the grid style, which controls how the grid is drawn over the map’s contents.

See also

style()

Parameters:

style (QgsLayoutItemMapGrid.GridStyle)

setUnits(self, unit: QgsLayoutItemMapGrid.GridUnit)[source]

Sets the unit to use for grid measurements such as the interval and offset for grid lines.

See also

units()

Parameters:

unit (QgsLayoutItemMapGrid.GridUnit)

style(self) QgsLayoutItemMapGrid.GridStyle[source]

Returns the grid’s style, which controls how the grid is drawn over the map’s contents.

See also

setStyle()

Return type:

QgsLayoutItemMapGrid.GridStyle

testFrameSideFlag(self, flag: QgsLayoutItemMapGrid.FrameSideFlag) bool[source]

Tests whether the grid frame should be drawn on a specified side of the map item.

Parameters:

flag (QgsLayoutItemMapGrid.FrameSideFlag) – flag for grid frame side

Return type:

bool

Returns:

True if grid frame should be drawn for that side of the map

See also

frameSideFlags()

units(self) QgsLayoutItemMapGrid.GridUnit[source]

Returns the units used for grid measurements such as the interval and offset for grid lines.

See also

setUnits()

Return type:

QgsLayoutItemMapGrid.GridUnit