2

I programmed one processing provider and four processing algorithms for a custom plugin. Loading the plugin through the Plugin Manager works fine, and the algorithms also run as expected when started.

However, I don't know how to clean up the processing provider when it's unloaded. I suspect it has to do with my MyProvider(QgsProcessingProvider)::unload() function, but I don't know what to change.

The template I used comes from the QGIS Plugin Builder. Since it creates one provider per algorithm, I changed the code and created a separate MyProvider class in its separate file. In its loadAlgorithms(), it loads all four algorithms, which work perfectly fine.

However, when the plugin is 'unloaded' through the Plugin Manager, neither the algorithms nor their provider disappear from the Processing Toolbox. Just their names disappear; the entries stay. When reloaded, their names don't show up, and they don't work anymore. The displayed error says:

_core.QgsProcessingException: Error creating algorithm from createInstance() 

I guess createInstance() fails because the name of the algorithm is missing, but what did I do wrong in the first place?

Some MyProvider class code:

from .algo1.algo1_algorithm import Algo1 class MyOMProvider(QgsProcessingProvider): def __init__(self): QgsProcessingProvider.__init__(self) def initGui(self): self.initProcessing() def initProcessing(self): QgsApplication.processingRegistry().addProvider(self) def loadAlgorithms(self): self.addAlgorithm(Algo1()) self.addAlgorithm(Algo2()) self.addAlgorithm(Algo3()) self.addAlgorithm(Algo4()) def unload(self): # What to do here? pass 

1 Answer 1

4

In general, this would usually be done in the unload() method of the main plugin class in the plugin.py file not the provider class.

I do not know what your files/classes are named but let's say you have a file called my_om_plugin.py and inside that file you have a class called MyOMPlugin e.g.

class MyOMPlugin(object): def __init__(self, iface): self.iface = iface self.provider = MyOMProvider() def initGui(self): QgsApplication.processingRegistry().addProvider(self.provider) def unload(self): QgsApplication.processingRegistry().removeProvider(self.provider) 

Note that the provider is instantiated inside the __init__() method and added to the processing registry inside the initGui() method. The provider should then be removed from the processing registry in the unload() method of the main plugin class. This is also where you would remove any associated actions from the plugin menu.

I have a simple example processing plugin in my GitHub here which you may find helpful.

1
  • 2
    Thanks Ben. I just had a provider class doing everything, but that doesn't work, now also obvious to me. Commented Apr 23 at 11:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.