To expand a little on the answer of @Fran Raga, a very quick way to add all the project vector layers to your layer combo box is to use a list comprehension:
vector_names = [l.name() for l in QgsProject().instance().mapLayers().values() if isinstance(l, QgsVectorLayer)]
However, I would really recommend looking into using the custom QGIS widgets
QgsFieldComboBox(). Once you start using them I don't think you will look back. A
QgsMapLayerComboBox shows all project layers by default, but it is ridiculously easy to filter to only vector layers, raster layers, layers which have geometry or any geometry type.
QgsFieldComboBox has a method
setLayer() which filters the fields shown to fields belonging to that layer. This makes it very simple to connect the
setLayer() method to the
layerChanged() signal of a
QgsMapLayerComboBox object and set the layer for the field combo box to the
currentLayer() of the map layer combo box so that whenever the layer combo box selection changes, the fields in the field combo box are updated dynamically.
As an added bonus you also get the nice little raster and vector icons next to the combo box items. You can also show the CRS of each layer.
try out this small snippet in the Python console in a project with a few different layers loaded. You should quickly get the idea of how it works and be able to adapt it into your plugins:
self.setGeometry(100, 100, 300, 200)
self.layout = QVBoxLayout(self)
self.cb_layer = QgsMapLayerComboBox(self)
self.cb_fields = QgsFieldComboBox(self)
Win = Dlg()
The custom widgets should be available in the QtDesigner which comes with QGIS. If you are on Windows & used the OSGeo4W installer you will find, in
C:\OSGeo4W64\bin, a batch file called either
qgis-ltr-designer depending on which version or versions you have installed. Double clicking on this batch file should launch QtDesigner with QGIS custom widgets.
If you can't access them through Designer for any reason, a workaround is to create and add them in your
plugin_dialog.py file. For example, add an empty layout in Designer, then create the combo box objects in
from qgis.gui import QgsMapLayerComboBox, QgsFieldComboBox
__init__() method, after
self.setupUi(self) something like:
self.cbLayer = QgsMapLayerComboBox(self)
self.cbFields = QgsFieldComboBox(self)
You should then be able to access and work with them in the
initGui() method of your main
plugin.py file e.g.