Class: QgsMapLayerComboBox¶
A combobox which displays a dynamic list of layers from a QGIS project.
QgsMapLayerComboBox
is automatically populated with all the
layers from the project. Any changes to the project’s layers are
immediately reflected in the combobox, such as addition of new layers,
layer renaming, or layer removal. Accordingly, this widget should ALWAYS
be used when presenting a choice of layers to a user, instead of
manually populated comboboxes.
In addition to the automatic layer population,
QgsMapLayerComboBox
also presents a user-friendly choice of
layers by showing standard icons representing the different layer types
(eg raster, mesh, or polygon/point/line icons for vector layers).
Helpful tooltips will be shown if the user hovers over any of the
layers, helping guide the user to the correct layer choice.
Optionally, the list of available layers can be filtered to a subset of
the project’s layers (such as only showing raster layers, or only
spatial layers) by calling setFilters()
.
By default, the combobox will be populated using layers from the current
project (see QgsProject.instance()
), however a specific
source project can be set via setProject()
.

QgsMapLayerComboBox in the collapsed state¶

QgsMapLayerComboBox in the expanded state¶
Class Hierarchy¶
Base classes¶
Methods
Returns the list of additional (non map layer) items included at the end of the combo box. |
|
Returns the list of additional layers added to the combobox. |
|
Returns |
|
Returns the current layer selected in the combo box. |
|
Returns a list of layers which should be excluded from the combo box. |
|
Returns the list of data providers which are excluded from the combobox. |
|
Returns any currently used filters on the listed layers. |
|
Returns the layer currently shown at the specified index within the combo box. |
|
Sets a list of additional (non map layer) items to include at the end of the combobox. |
|
Sets a list of additional layers to include in the combobox. |
|
Sets whether an optional empty layer ("not set") option is shown in the combo box. |
|
Sets a list of layers which should be excluded from the combo box. |
|
Sets a list of data providers which should be excluded from the combobox. |
|
Sets filters for the layers displayed in the combo box. |
|
Sets the current layer selected in the combo box. |
|
Sets the project from which map layers are shown. |
|
Sets whether the CRS of layers is also included in the combo box text. |
|
Returns |
Signals
Emitted whenever the currently selected layer changes. |
- class qgis.gui.QgsMapLayerComboBox[source]¶
Bases:
QComboBox
- __init__(parent: QWidget | None = None)
QgsMapLayerComboBox creates a combo box to display the list of layers currently in the project.
The layers can be filtered and/or ordered.
- Parameters:
parent (Optional[QWidget] = None)
- additionalItems(self) List[str] [source]¶
Returns the list of additional (non map layer) items included at the end of the combo box.
See also
- Return type:
List[str]
- additionalLayers(self) List[QgsMapLayer] ¶
Returns the list of additional layers added to the combobox.
See also
Added in version 3.22.
- Return type:
List[QgsMapLayer]
- allowEmptyLayer(self) bool [source]¶
Returns
True
if the combo box allows the empty layer (“not set”) choice.See also
- Return type:
bool
- currentLayer(self) QgsMapLayer | None [source]¶
Returns the current layer selected in the combo box.
See also
- Return type:
Optional[QgsMapLayer]
- exceptedLayerList(self) List[QgsMapLayer] ¶
Returns a list of layers which should be excluded from the combo box.
See also
- Return type:
List[QgsMapLayer]
- excludedProviders(self) List[str] [source]¶
Returns the list of data providers which are excluded from the combobox.
See also
- Return type:
List[str]
- filters(self) Qgis.LayerFilters [source]¶
Returns any currently used filters on the listed layers.
See also
- Return type:
- layer(self, layerIndex: int) QgsMapLayer | None [source]¶
Returns the layer currently shown at the specified index within the combo box.
- Parameters:
layerIndex (int) – position of layer to return
See also
- Return type:
Optional[QgsMapLayer]
- signal layerChanged(layer: QgsMapLayer)[source]¶
Emitted whenever the currently selected layer changes.
- Parameters:
layer (QgsMapLayer)
- setAdditionalItems(self, items: Iterable[str | None])[source]¶
Sets a list of additional (non map layer) items to include at the end of the combobox. These may represent additional layers such as layers which are not included in the project, or paths to layers which have not yet been loaded into QGIS.
See also
- Parameters:
items (Iterable[Optional[str]])
- setAdditionalLayers(self, layers: Iterable[QgsMapLayer])[source]¶
Sets a list of additional
layers
to include in the combobox.This method allows adding additional layers, which are not part of a project’s layers, into the combobox.
See also
Added in version 3.22.
- Parameters:
layers (Iterable[QgsMapLayer])
- setAllowEmptyLayer(self, allowEmpty: bool, text: str | None = '', icon: QIcon = QIcon())[source]¶
Sets whether an optional empty layer (“not set”) option is shown in the combo box.
Since QGIS 3.20, the optional
text
andicon
arguments allows the text and icon for the empty layer item to be set.See also
- Parameters:
allowEmpty (bool)
text (Optional[str] = '')
icon (QIcon = QIcon())
- setExceptedLayerList(self, layerList: Iterable[QgsMapLayer])[source]¶
Sets a list of layers which should be excluded from the combo box.
See also
- Parameters:
layerList (Iterable[QgsMapLayer])
- setExcludedProviders(self, providers: Iterable[str | None])[source]¶
Sets a list of data providers which should be excluded from the combobox.
See also
- Parameters:
providers (Iterable[Optional[str]])
- setFilters(self, filters: Qgis.LayerFilters | Qgis.LayerFilter)[source]¶
Sets
filters
for the layers displayed in the combo box.This method allows filtering layers according to layer type and/or geometry type.
See also
- Parameters:
filters (Union[Qgis.LayerFilters, Qgis.LayerFilter])
- setFilters(self, filters: int)[source]
Filters according to layer type and/or geometry type.
Note
for API compatibility
Added in version 3.34.
Deprecated since version 3.34: Use the flag signature instead.
- Parameters:
filters (int)
- setLayer(self, layer: QgsMapLayer | None)[source]¶
Sets the current
layer
selected in the combo box.- Parameters:
layer (Optional[QgsMapLayer])
- setProject(self, project: QgsProject | None)[source]¶
Sets the
project
from which map layers are shown.If
project
isNone
thenQgsProject.instance()
will be used.Added in version 3.24.
- Parameters:
project (Optional[QgsProject])
- setShowCrs(self, showCrs: bool)[source]¶
Sets whether the CRS of layers is also included in the combo box text.
See also
- Parameters:
showCrs (bool)