0

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
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 27, 2021 at 5:12
2
  • @PolyGeo, can you assist me, please. I really need help here. Commented 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. Commented Apr 5, 2021 at 4:46

1 Answer 1

0
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.

answered Jul 20, 2021 at 6:52
2
  • Can you provide more written information on what you were trying to achieve ? Commented Jul 20, 2021 at 11:18
  • The index of the QgsMapLayerComboBox may not necessarily match the index in the QgsLayerTree. Commented Sep 17, 2024 at 14:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.