I am building a plugin and so far I have tried simplifying the tasks. But my greatest challenge is making sure that the selected layer in the "mMapLayerComboBox" is what is exactly used in the operation shown below:
def run(self):
mains = self.dlg.mMapLayerComboBox.currentLayer() ### vector layer
laterals = self.dlg.mMapLayerComboBox_2.currentLayer() ### vector layer
ovalay = self.dlg.mMapLayerComboBox_3.currentLayer() ### Raster layer
for layer in checkedLayers:
if layer.type() == QgsMapLayer.VectorLayer and self.dlg.checkBox_3.isChecked():
# get extension about the raster
filename, file_extension = os.path.splitext(layer.source())
output1 = self.folderName + "/Profiles/m-length_" + layer.name() + ".shp"
processing.run("qgis:exportaddgeometrycolumns", {"INPUT": mains, "CALC_METHOD": 0, "OUTPUT": output1})
In the "INPUT", I had specified the layer to use. However, while the task works, the main problem is that the task executes for all loaded vector layers in QGIS, even if I have a basemap like from google satellite it just tries to run the task on all available vector layers instead of the one I specified as:
mains = self.dlg.mMapLayerComboBox.currentLayer() ### vector layer
What I get as an output is a result for all the loaded layers including the specified layer in the "mMapLayerComboBox". I just want one output and not for all loaded vector or raster layers as the case may be.
How do I resolve this problem when using "mMapLayerComboBox" from QGIS built-in custom widget?
I want my task to only perform operations on the selected layer, that's all.
-
@PolyGeo, can you assist me, please. I really need help here.Beginner– Beginner2021年04月05日 03:48:57 +00:00Commented Apr 5, 2021 at 3:48
-
Sorry @Beginner. I'm not a potential answerer of your question but I just added a tag for PyQGIS which should help it reach the filters of some of our volunteers who may be.PolyGeo– PolyGeo ♦2021年04月05日 04:46:24 +00:00Commented Apr 5, 2021 at 4:46
1 Answer 1
layers = QgsProject.instance().layerTreeRoot().children()
layerindex = self.dlg.mMapLayerComboBox.currentIndex()
layer = layers[layerindex]
print(layer)
Python Console: <qgis._core.QgsLayerTreeLayer object at 0x000001DD6DD8FD38
I did like this and one layer object was printed on python console.
-
Can you provide more written information on what you were trying to achieve ?Pierrick Rambaud– Pierrick Rambaud2021年07月20日 11:18:31 +00:00Commented Jul 20, 2021 at 11:18
-
The index of the
QgsMapLayerComboBox
may not necessarily match the index in theQgsLayerTree
.Matt– Matt2024年09月17日 14:08:59 +00:00Commented Sep 17, 2024 at 14:08
Explore related questions
See similar questions with these tags.