0

I have created a plugin with two separate algorithms. When I open one algorithm (either under plugins menu or through the button at the top) the second one also opens automatically. However, if I open an algorithm in the processing toolbox only the one selected opens. How do I stop both algorithms opening at the same time? Is it related to def unload?

class PluginPlugin(object):

def __init__(self, iface): self.provider = None self.iface = iface def initProcessing(self): """Init Processing provider for QGIS >= 3.8.""" self.provider = PluginProvider() QgsApplication.processingRegistry().addProvider(self.provider) def initGui(self): self.initProcessing() icon = os.path.join(os.path.join(cmd_folder, 'logo.png')) self.action = QAction( QIcon(icon), u"Algorithm 1", self.iface.mainWindow()) self.action.triggered.connect(self.run) self.iface.addPluginToMenu(u"&Plugin", self.action) self.iface.addToolBarIcon(self.action) self.action = QAction( QIcon(icon), u"Algorithm 2", self.iface.mainWindow()) self.action.triggered.connect(self.run) self.iface.addPluginToMenu(u"&Plugin", self.action) self.iface.addToolBarIcon(self.action) def unload(self): QgsApplication.processingRegistry().removeProvider(self.provider) self.iface.removePluginMenu(u"&Plugin", self.action) self.iface.removeToolBarIcon(self.action) def run(self): processing.execAlgorithmDialog("Plugin:Algorithm 1") processing.execAlgorithmDialog("Plugin:Algorithm 2") """Run method that performs all the real work""" 

1 Answer 1

3

It's doing exactly what you are telling it to do- not what you want it to do!

You are creating 2 identical QAction objects- when you define the second self.action it redefines (overwrites) the first. This action is connected to the run method, and in the run method you are asking for both dialogs to be opened- so of course both dialogs are going to be opened.

What you need to do is to create 2 different actions, whose triggered signals are connected to 2 different slot methods.

I have not tested it, but the modified code below should work for you, though you will probably want to create another icon (both actions here will have exactly the same icon).

class PluginPlugin(object): def __init__(self, iface): self.provider = None self.iface = iface def initProcessing(self): """Init Processing provider for QGIS >= 3.8.""" self.provider = PluginProvider() QgsApplication.processingRegistry().addProvider(self.provider) def initGui(self): self.initProcessing() icon = os.path.join(os.path.join(cmd_folder, 'logo.png')) self.action_1 = QAction( QIcon(icon), u"Algorithm 1", self.iface.mainWindow()) self.action_1.triggered.connect(self.runAlg_1) self.iface.addPluginToMenu(u"&Plugin", self.action_1) self.iface.addToolBarIcon(self.action_1) self.action_2 = QAction( QIcon(icon), u"Algorithm 2", self.iface.mainWindow()) self.action_2.triggered.connect(self.runAlg_2) self.iface.addPluginToMenu(u"&Plugin", self.action_2) self.iface.addToolBarIcon(self.action_2) def unload(self): QgsApplication.processingRegistry().removeProvider(self.provider) self.iface.removePluginMenu(u"&Plugin", self.action_1) self.iface.removePluginMenu(u"&Plugin", self.action_2) self.iface.removeToolBarIcon(self.action_1) self.iface.removeToolBarIcon(self.action_2) def runAlg_1(self): processing.execAlgorithmDialog("Plugin:Algorithm 1") def runAlg_2(self): processing.execAlgorithmDialog("Plugin:Algorithm 2") 

I would definitely recommend studying PyQt GUI development topics such as QActions as well as signal and slot event handling. There are plenty of tutorials online as well as videos on you tube.

3
  • I have run the above script, however I get the following error message: self.action.triggered.connect(self.runAlg_1) AttributeError: 'Plugin' object has no attribute 'action' Commented Aug 23, 2022 at 10:54
  • @GISJess, Ok, couple of small changes I missed. I've edited my answer. Please try it again. Commented Aug 23, 2022 at 10:59
  • Problem resolved. Commented Aug 23, 2022 at 12:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.