I want to create a simple QGIS plugin with a simple combo box with list from my layers

in _ _init_ _ :

    legendInterface = self.iface.legendInterface()
    listLayerName = [i.name() for i in legendInterface.layers() if i.type() == QgsMapLayer.VectorLayer]
    # add all these layer names to combo box comboLayerList

but if I start plugin in QGIS then the combo box not working is empty:

enter image description here

if I reload my plugin use reload using Reload plugin :

enter image description here

then the combo box is working. If I close QGIS and start again then combo box is not working again. If I again reload plugin then works.

Where is my mistake and my combo box not working? how to refresh the plugin on start to work combo box?

  • 1
    I second @ndawson suggestion of using a QgsMapLayerComboBox. But if you want to stick with your comboboxes, take your code out of the __init__ method as this only runs when the plugin is loaded. Assuming you used the Plugin Builder plugin, you can put your code in the run() function which is executed when you run your plugin. You should also include self.ui.comboBox_2.clear() before self.ui.comboBox_2.addItems(listLayerName) to avoid duplicates.
    – Joseph
    Jan 30, 2018 at 10:40
  • @ Joseph yes but I hard to do this now because I start without Plugin Builder plugin I will try again
    – jessie jes
    Jan 31, 2018 at 3:13

1 Answer 1


Don't make your own - PyQGIS has a whole library of standard reusable widgets like this. In this case use QgsMapLayerComboBox. All the hard work is done for you, it's stable and unit tested, and will keep UI consistency with the rest of the QGIS interface.

  • I try to follow youe proposal and I have add QgisMapLayerComboBox in my ui using QT designer but I take this error :Couldn't load plugin ImportError: No module named qgsmaplayercombobox finaly I try to import from qgis.gui import QgsMapLayerComboBox but without success I have same error
    – jessie jes
    Jan 31, 2018 at 3:09
  • Which QGIS version are you using?
    – ndawson
    Jan 31, 2018 at 6:12
  • I use QGIS 2.14.20
    – jessie jes
    Jan 31, 2018 at 14:20
  • How are you creating the QgsMapLayerComboBox in Qt Designer? I usually create a new QComboBox and then promote it for example to QgsScaleComboBox.
    – Blinxen
    Feb 1, 2018 at 11:01
  • @Blinxen yes i use Qt Designe how to promote ?
    – jessie jes
    Feb 25, 2018 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.