Since you are populating your combo boxes with layers which are loaded in the current project, in the context of a QGIS plugin you will probably find that it is easier in most instances to work directly with the map layer object than the source file.
For example, you can get a map layer object from your combo box (e.g. in run() method) like this:
raster_name = self.dlg.comboBox_rst.currentText()
raster_layer = QgsProject().instance().mapLayersByName(raster_name)
#Do something with raster_layer
Then, if you do need to get the path to the source file, just call
source() on the
path = raster_layer.source()
However, for selecting QGIS specific items like layers, fields etc. I would recommend looking at using custom QGIS widgets like
These are available in the Qt Designer which comes with QGIS. If you installed with OSGeo4W installer, you should find a batch file in C:\OSGeo4W64\bin (it looks like you are on Windows) with the name
qgis-ltr-designer. Double clicking this batch file should launch Qt Designer complete with QGIS custom widgets.
self.dlg.comboBox_rst object is a
QgsMapLayerComboBox instead of a regular
QComboBox, you will now get the nice little raster and vector icons next to the layer names. You don't have to create a list of loaded layers and add them as items to the combo box- a
QgsMapLayerComboBox contains all project layers by default, but you can easily apply a filter to show only raster or vector layers:
Or show the layer CRS:
Which will give you a result like:
Getting the map layer object from the combo box is as easy as:
raster_layer = self.dlg.comboBox_rst.currentLayer()
By the way, if you are unable to access the custom widgets through Designer for any reason, a workaround is to just add an empty layout in Designer then create and add custom widgets to it later e.g. by modifying your plugin dialog file.
#Add to import statements
from qgis.gui import QgsMapLayerComboBox
#Add to __init__() method after setupUi
self.comboBox_rst = QgsMapLayerComboBox(self)
You can then access these widgets as usual in the
initGui() method of your main plugin file:
And finally, just as a bit of food for thought- if your aim is to teach yourself plugin development then by all means, go for it and have fun!! But for most practical purposes, if it's a processing task that's required, then a QGIS processing script is the way to go. They are actually quicker and easier to develop once you get your head around the basics, and can be added to the processing toolbox which means they can be used in models etc.
You can even package a collection of custom scripts into a processing plugin which will add a new provider to the tool box.
Here are some links to get you going: