[QGIS-Developer] Add algorithm to an existing provider?

Benjamin Jakimow benjamin.jakimow at geo.hu-berlin.de
Fri May 13 03:07:52 PDT 2022


Hi Raymond,

what is mean is something like:

class TestProvider():

    def __init__(...):

       ...
       self.mMyAlgorithms=[]

    def addAlgorithm(a):

       super().addAlgorithm(a)
       self.mMyAlgorithms.append(a)  # <-- keep a python reference
       self.algorithmsLoaded.emit()  # <-- required to inform others, 
e.g. the model builder

With the python reference the TestAlgorithm instance should stay alive 
as long as the TestProvider exists.

Note that each time self.algorithmsLoaded.emit() other models, e.g. the 
QgsProcessingToolboxProxyModel, might want to reload all algorithms from 
all providers.
If your provider is used to add or remove multiple algorithms more 
frequently, it is good to emit algorithmsLoaded only once after all 
algorithms have been added/removed.

Greetings,
Benjamin

On 2022-05-13 11:48, Raymond Nijssen via QGIS-Developer wrote:
> Hi Benjamin, thanks for your reply.
> 
> I'm not sure how the provider should "save a reference on my
> algorithm". What do you mean?
> 
> Some things I tried:
> 
> 
> self.provider = TestProvider()
> QgsApplication.processingRegistry().addProvider(self.provider)
> 
> alg2 = TestAlgorithm2()
> alg2.setProvider(self.provider) # Maybe this is "setting a reference??
> 
> self.provider.addAlgorithm(alg2)
> 
> # Now the provider shows the extra algorithms (but not in the GUI)
> print(self.provider.algorithms())
> 
> # Attempts to get it in the GUI:
> self.provider.refreshAlgorithms()
> self.provider.loadAlgorithms()
> self.provider.load()
> 
> 
> Still no luck!
> 
> Regards,
> Raymond
> 
> 
> On 13-05-2022 09:07, Benjamin Jakimow wrote:
>> Hi Raymond,
>> 
>> you need to ensure that your provider saves a python reference on your 
>> TestAlgorithm2() (which you probably already do).
>> After its set of algorithms has changed (i.e. refresh, addAlgorithm(s) 
>> etc.) it needs to emit the algorithmsLoaded signal.
>> 
>> Greetings,
>> Benjamin
>> 
>> On 2022-05-13 08:15, Raymond Nijssen via QGIS-Developer wrote:
>>> Is there a way to add a processing algorithm to an existing provider
>>> using pyqgis?
>>> 
>>> I have 2 plugins and don't know if they are both installed (or which
>>> one will load first). The both contain processing algorithms and I
>>> want those to end up in the same group.
>>> 
>>> Documentation is a bit brief on this:
>>> 
>>> QgsProcessingProvider
>>> 
>>> Examples show to put the addAlgorithm() function(s) in the
>>> loadAlgorithms() function. Then calling
>>> 
>>> QgsApplication.processingRegistry().addProvider(provider)
>>> 
>>> adds the group (=provider?) and the algorithms are in it.
>>> 
>>> 
>>> 
>>> What I want to do is finding the provider if it exists already:
>>> 
>>> self.provider = 
>>> QgsApplication.processingRegistry().providerById('test')
>>> if self.provider is None:
>>>     self.provider = RayProvider()
>>> 
>>> and then:
>>> 
>>> self.provider.addAlgorithm(TestAlgorithm2())
>>> 
>>> This doesn't work, neither when doing:
>>> 
>>> provider.refreshAlgorithms()
>>> provider.load()
>>> provider.loadAlgorithms()
>>> ...
>>> 
>>> 
>>> I'm currently trial-and-errorring my way through this, but I hope
>>> someone has a hint. Even someone pointing at 2 plugins working like
>>> this would be very welcome!
>>> 
>>> Kind regards,
>>> Raymond
>>> _______________________________________________
>>> QGIS-Developer mailing list
>>> QGIS-Developer at lists.osgeo.org
>>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> 
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

-- 
--
Benjamin Jakimow, Doctoral Researcher
Earth Observation Lab | Geography Department | Humboldt-Universität zu 
Berlin

e-mail: benjamin.jakimow at geo.hu-berlin.de

phone:  +49 (0) 30 2093 6894
mobile: +49 (0) 157 5656 8477
fax:    +49 (0) 30 2093 6848
mail:   Unter den Linden 6 | 10099 Berlin | Germany
room: 2'222


More information about the QGIS-Developer mailing list