Class: QgsMapThemeCollection

Container class that allows storage of map themes consisting of visible map layers and layer styles.

Class Hierarchy

Inheritance diagram of qgis.core.QgsMapThemeCollection

Base classes

QObject

Methods

applyTheme

Apply theme given by its name and modify layer tree, current style of layers and checked legend items of passed layer tree model.

clear

Removes all map themes from the collection.

hasMapTheme

Returns whether a map theme with a matching name exists.

insert

Inserts a new map theme to the collection.

mapThemeState

Returns the recorded state of a map theme.

mapThemeStyleOverrides

Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.

mapThemeVisibleLayerIds

Returns the list of layer IDs that are visible for the specified map theme.

mapThemeVisibleLayers

Returns the list of layers that are visible for the specified map theme.

mapThemes

Returns a list of existing map theme names.

masterLayerOrder

Returns the master layer order (this will always match the project's QgsProject.layerOrder() ).

masterVisibleLayers

Returns the master list of visible layers.

project

Returns the QgsProject on which this map theme collection works.

readXml

Reads the map theme collection state from XML

removeMapTheme

Removes an existing map theme from collection.

renameMapTheme

Renames the existing map theme called name to newName.

setProject

Sets the project on which this map theme collection works.

update

Updates a map theme within the collection.

writeXml

Writes the map theme collection state to XML.

Static Methods

createThemeFromCurrentState

Static method to create theme from the current state of layer visibilities in layer tree, current style of layers and check state of legend items (from a layer tree model).

Signals

mapThemeChanged

Emitted when a map theme changes definition.

mapThemeRenamed

Emitted when a map theme within the collection is renamed.

mapThemesChanged

Emitted when map themes within the collection are changed.

projectChanged

Emitted when the project changes

class qgis.core.QgsMapThemeCollection[source]

Bases: QObject

__init__(project: QgsProject | None = None)

Create map theme collection that handles themes of the given project.

Parameters:

project (Optional[QgsProject] = None)

class MapThemeLayerRecord[source]

Bases: object

Individual record of a visible layer in a map theme record.

QgsMapThemeCollection.MapThemeLayerRecord(l: Optional[QgsMapLayer] = None) Initialize layer record with a map layer - it will be stored as a weak pointer

QgsMapThemeCollection.MapThemeLayerRecord(a0: QgsMapThemeCollection.MapThemeLayerRecord)

checkedLegendItems: Set[str]

Rule keys of check legend items in layer tree model

currentStyle: str

Name of the current style of the layer

expandedLayerNode: bool

Whether the layer’s tree node is expanded (only to be applied if the parent MapThemeRecord has the information about expanded nodes stored)

Added in version 3.2.

expandedLegendItems: Set[str]

Rule keys of expanded legend items in layer tree view.

Added in version 3.2.

isVisible: bool

True if the layer is visible in the associated theme.

Added in version 3.14.

layer(self) QgsMapLayer | None[source]

Returns map layer or None if the layer does not exist anymore

Return type:

Optional[QgsMapLayer]

setLayer(self, layer: QgsMapLayer | None)[source]

Sets the map layer for this record

Parameters:

layer (Optional[QgsMapLayer])

usingCurrentStyle: bool

Whether current style is valid and should be applied

usingLegendItems: bool

Whether checkedLegendItems should be applied

class MapThemeRecord[source]

Bases: object

Individual map theme record of visible layers and styles.

addLayerRecord(self, record: QgsMapThemeCollection.MapThemeLayerRecord)[source]

Add a new record for a layer.

Parameters:

record (QgsMapThemeCollection.MapThemeLayerRecord)

checkedGroupNodes(self) Set[str]

Returns a set of group identifiers for group nodes that should have checked state (other group nodes should be unchecked). The returned value is valid only when hasCheckedStateInfo() returns True. Group identifiers are built using group names, a sub-group name is prepended by parent group’s identifier and a forward slash, e.g. “level1/level2”

Added in version 3.10.1.

Return type:

Set[str]

expandedGroupNodes(self) Set[str]

Returns a set of group identifiers for group nodes that should have expanded state (other group nodes should be collapsed). The returned value is valid only when hasExpandedStateInfo() returns True. Group identifiers are built using group names, a sub-group name is prepended by parent group’s identifier and a forward slash, e.g. “level1/level2”

Added in version 3.2.

Return type:

Set[str]

hasCheckedStateInfo(self) bool[source]

Returns whether information about checked/unchecked state of groups has been recorded and thus whether checkedGroupNodes() is valid.

Added in version 3.10.1.

Return type:

bool

hasExpandedStateInfo(self) bool[source]

Returns whether information about expanded/collapsed state of nodes has been recorded and thus whether expandedGroupNodes() and expandedLegendItems + expandedLayerNode from layer records are valid.

Added in version 3.2.

Return type:

bool

layerRecords(self) List[QgsMapThemeCollection.MapThemeLayerRecord]

Returns a list of records for all visible layer belonging to the theme.

Return type:

List[QgsMapThemeCollection.MapThemeLayerRecord]

static readXml(element: QDomElement, project: QgsProject | None) QgsMapThemeCollection.MapThemeRecord[source]

Reads the map theme record from XML

Parameters:
  • element (QDomElement) – DOM element

  • project (Optional[QgsProject]) – the QGIS project

See also

writeXml()

Added in version 3.42.

Return type:

QgsMapThemeCollection.MapThemeRecord

removeLayerRecord(self, layer: QgsMapLayer | None)[source]

Removes a record for layer if present.

Parameters:

layer (Optional[QgsMapLayer])

setCheckedGroupNodes(self, checkedGroupNodes: Iterable[str | None])[source]

Sets a set of group identifiers for group nodes that should have checked state. See checkedGroupNodes().

Added in version 3.10.1.

Parameters:

checkedGroupNodes (Iterable[Optional[str]])

setExpandedGroupNodes(self, expandedGroupNodes: Iterable[str | None])[source]

Sets a set of group identifiers for group nodes that should have expanded state. See expandedGroupNodes().

Added in version 3.2.

Parameters:

expandedGroupNodes (Iterable[Optional[str]])

setHasCheckedStateInfo(self, hasInfo: bool)[source]

Sets whether the map theme contains valid checked/unchecked state of group nodes

Added in version 3.10.1.

Parameters:

hasInfo (bool)

setHasExpandedStateInfo(self, hasInfo: bool)[source]

Sets whether the map theme contains valid expanded/collapsed state of nodes

Added in version 3.2.

Parameters:

hasInfo (bool)

setLayerRecords(self, records: Iterable[QgsMapThemeCollection.MapThemeLayerRecord])[source]

Sets layer records for the theme.

Parameters:

records (Iterable[QgsMapThemeCollection.MapThemeLayerRecord])

writeXml(self, element: QDomElement, document: QDomDocument)[source]

Writes the map theme record to XML.

Parameters:
  • element (QDomElement) – DOM element

  • document (QDomDocument) – DOM document

See also

readXml()

Added in version 3.42.

applyTheme(self, name: str | None, root: QgsLayerTreeGroup | None, model: QgsLayerTreeModel | None)[source]

Apply theme given by its name and modify layer tree, current style of layers and checked legend items of passed layer tree model.

Parameters:
clear(self)[source]

Removes all map themes from the collection.

static createThemeFromCurrentState(root: QgsLayerTreeGroup | None, model: QgsLayerTreeModel | None) QgsMapThemeCollection.MapThemeRecord[source]

Static method to create theme from the current state of layer visibilities in layer tree, current style of layers and check state of legend items (from a layer tree model).

Parameters:
Return type:

QgsMapThemeCollection.MapThemeRecord

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

Returns whether a map theme with a matching name exists.

Parameters:

name (Optional[str])

Return type:

bool

insert(self, name: str | None, state: QgsMapThemeCollection.MapThemeRecord)[source]

Inserts a new map theme to the collection.

See also

update()

Parameters:
signal mapThemeChanged(theme: str)[source]

Emitted when a map theme changes definition.

Parameters:

theme (str)

signal mapThemeRenamed(name: str, newName: str)[source]

Emitted when a map theme within the collection is renamed.

Added in version 3.14.

Parameters:
  • name (str)

  • newName (str)

mapThemeState(self, name: str | None) QgsMapThemeCollection.MapThemeRecord[source]

Returns the recorded state of a map theme.

Parameters:

name (Optional[str])

Return type:

QgsMapThemeCollection.MapThemeRecord

mapThemeStyleOverrides(self, name: str | None) Dict[str, str]

Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.

Parameters:

name (Optional[str])

Return type:

Dict[str, str]

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

Returns the list of layer IDs that are visible for the specified map theme.

Note

The order of the returned list is not guaranteed to reflect the order of layers in the canvas.

Parameters:

name (Optional[str])

Return type:

List[str]

mapThemeVisibleLayers(self, name: str | None) List[QgsMapLayer]

Returns the list of layers that are visible for the specified map theme.

Note

The order of the returned list is not guaranteed to reflect the order of layers in the canvas.

Parameters:

name (Optional[str])

Return type:

List[QgsMapLayer]

mapThemes(self) List[str][source]

Returns a list of existing map theme names.

Return type:

List[str]

signal mapThemesChanged[source]

Emitted when map themes within the collection are changed.

masterLayerOrder(self) List[QgsMapLayer]

Returns the master layer order (this will always match the project’s QgsProject.layerOrder() ). All map themes will maintain the same layer order as the master layer order.

Return type:

List[QgsMapLayer]

masterVisibleLayers(self) List[QgsMapLayer]

Returns the master list of visible layers. The order of returned layers will always match those of masterLayerOrder(), but the returned layers are filtered to only include those visible in the project’s layer tree.

Return type:

List[QgsMapLayer]

project(self) QgsProject | None[source]

Returns the QgsProject on which this map theme collection works.

See also

setProject()

See also

projectChanged()

Return type:

Optional[QgsProject]

signal projectChanged[source]

Emitted when the project changes

See also

project()

See also

setProject()

readXml(self, doc: QDomDocument)[source]

Reads the map theme collection state from XML

Parameters:

doc (QDomDocument) – DOM document

See also

writeXml()

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

Removes an existing map theme from collection.

Parameters:

name (Optional[str])

renameMapTheme(self, name: str | None, newName: str | None) bool[source]

Renames the existing map theme called name to newName. Returns True if the rename was successful, or False if it failed (e.g. due to a duplicate name for newName).

Added in version 3.14.

Parameters:
  • name (Optional[str])

  • newName (Optional[str])

Return type:

bool

setProject(self, project: QgsProject | None)[source]

Sets the project on which this map theme collection works.

See also

project()

See also

projectChanged()

Parameters:

project (Optional[QgsProject])

update(self, name: str | None, state: QgsMapThemeCollection.MapThemeRecord)[source]

Updates a map theme within the collection.

Parameters:

See also

insert()

writeXml(self, doc: QDomDocument)[source]

Writes the map theme collection state to XML.

Parameters:

doc (QDomDocument) – DOM document

See also

readXml()