I'm using a qgis plugin with very basic gui with 4 QgsMapLayerComboBoxes, I'll use each one to select a layer. I want to retrieve the currently selected item's name for each combobox (a vector layer) as a variable in my function so I can edit each layer.
As of right now, I added this code to my def run(self) :
self.dlg.mapLayerComboBox1.setFilters(QgsMapLayerProxyModel.PolygonLayer) self.dlg.mapLayerComboBox2.setFilters(QgsMapLayerProxyModel.LineLayer) self.dlg.mapLayerComboBox3.setFilters(QgsMapLayerProxyModel.PointLayer) self.dlg.mapLayerComboBox4.setFilters(QgsMapLayerProxyModel.PointLayer)
In my function I have this
layer1 = mapLayerComboBox1.layer() (same with other layers), I'm not sure yet how to retrieve the layer's name (to use with
mapLayersByName similarly to how it would be feasible using
QComboBox.currentText() something I learned reading PyQGIS using selected layer from a combobox) but even before that I receive the following error when I test the plugin:
layer1 = mapLayerComboBox1.layer() NameError: name 'mapLayerComboBox1' is not defined
At the start of my MainPlugin.py I added this
from qgis.gui import QgsMapLayerComboBox following answers from this thread Debugging Global name ''QgsMapLayerComboBox' is not defined? but I still get the error. Following the second answer (which advises to use from
YourPluginName_dialog_base import QgsMapLayerComboBox) I receive a critical error:
File "C:/OSGEO4~1/apps/qgis-ltr/./python\qgis\utils.py", line 793, in _import mod = _builtin_import(name, globals, locals, fromlist, level) ModuleNotFoundError: No module named 'mypluginname_dialog_base'