I have a combobox in my plugin (python Qt dockable widget) that needs to contain the currently open rasters.

How do I trigger an update of the plugin when something new gets added to the map?


There's a couple of methods which I use (note that it may not be the most efficient solution):

  • (using Combo Box) - Add the following line before you populate your combo box:


    Then when you add a new layer, close your plugin and open it again, the combo box should be re-populated with the new additions.

  • (using QgsMapLayerComboBox) - This is automatically updated when any changes are made to the layer list (i.e. layers added/removed, names changed etc) without you having to refresh the plugin. I would recommend this method. You can set it so that it only recognises raster layers by using something like:

    self.dockwidget.mapLayerComboBox.setFilters( QgsMapLayerProxyModel.RasterLayer )

Note: When using QGIS custom widgets, you may need to edit your dockwidget_base.ui file, find the QgsMapLayerComboBox class (usually at the end of the file) and replace the <header> to qgis.gui.h.

  • 1
    This is great. I didn't even know there were custom QGIS controls. Are there others? I couldn't find a list of them anywhere. – Raychaser Jun 21 '16 at 15:58
  • @Raychaser - Awesome! Hmm only ones I know are those shown in the QT Designer, can't seem to find documentation for a list of them though. – Joseph Jun 22 '16 at 8:29

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.