Sunday, 24 September 2017

pyqgis - How can I trigger a QGIS plugin Event from map refresh?


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?



Answer



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:


    self.dockwidget.comboBox.clear()

    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

to qgis.gui.h.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...