I am creating a temporary plugin for QGIS using the QGIS plugin builder plugin. Aim is to learn how to make plugins in QGIS. I have added two comboBoxes that allows the user to select a vector layer and a raster layer respectively. The plugin will process the data specified by the user in these comboboxes. For that I need to extract the entire path and name of the layers selected by the user. This might probably seem quite a basic post but I tried looking up for this, and failed to find a solution even after playing around with several codes. Below is the code for the comboBoxes:
# Getting the loaded raster layers
layerlist_rst = ['<Select a raster layer>']
layers_rst = QgsProject.instance().mapLayers().values()
for layer in layers_rst:
if layer.type() == QgsMapLayer.RasterLayer:
layerlist_rst.append(layer.name())
# Getting the loaded vector layers
layerlist_vect = ['<Select a vector layer>']
layers_vect = QgsProject.instance().mapLayers().values()
for layer in layers_vect:
if layer.type() == QgsMapLayer.VectorLayer:
layerlist_vect.append(layer.name())
#Clear contents of the comboBox from previous runs
self.dlg.comboBox_rst.clear() #This combobox loads raster layers
#Populate the comboBox with names of all the loaded raster layers
self.dlg.comboBox_rst.addItems(layerlist_rst)
#Clear contents of the comboBox from previous runs
self.dlg.comboBox_vect.clear() #This combobox loads the vector layers
self.dlg.comboBox_vect.addItems(layerlist_vect)
Now I need to get the names and path of the selected layers as text. Example: If the layer added by user is 'C\Users\XYZ\Rstr.tif' I want to assign this path to a variable that can be used later in the plugin like: raster = r'C\Users\XYZ\Rstr.tif'