<div dir="ltr"><div dir="ltr"><div>Dear list,</div><div><br></div><div>I'm currently working on a plugin where I need to disable some actions from the legend context menu (the one that appears when right-clicking).</div><div><br></div><div>I though of doing so by creating a custom QgsLayerTreeViewMenuProvider  class that would get the QMenu from the default menu provider, then do the alterations, then return the altered QMenu.</div><div><br></div><div>I was almost able to get it working like this (stripped down example below). This works the first time, when enabling the plugin in the plugin manager. It also works from the Python console. But when the plugin is loaded at the starting of QGIS, I get a hard crash upon right-clicking in the legend.</div><div><br></div><div>Everything looks like either the layer tree view or the default menu provider is not correctly initialized when the plugin's initGui is called.</div><div><br></div><div>I tried some workarounds, such as retrieving and replacing the provider a bit later (using iface.projectRead or iface.currentLayerChanged signals), which mitigates the issue, but I still get random crashes (around 1 out of 5-6 times).</div><div><br></div><div>Is there something I'm doing wrong ? Or can you think of another way to achieve my goal ?</div><div><br></div><div>Thanks !!</div><div><br></div><div>Olivier<br></div><div><br></div><div>

<div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas,"Courier New",monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> qgis.PyQt.QtWidgets </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> QAction</span></div><div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> qgis.gui </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> QgsLayerTreeViewMenuProvider</span></div><div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> qgis.core </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> QgsMessageLog</span></div><br><div><span style="color:rgb(86,156,214)">class</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">CustomMenuProvider</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(78,201,176)">QgsLayerTreeViewMenuProvider</span><span style="color:rgb(212,212,212)">):</span></div><br><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">__init__</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(156,220,254)">old_provider</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(78,201,176)">super</span><span style="color:rgb(212,212,212)">().</span><span style="color:rgb(220,220,170)">__init__</span><span style="color:rgb(212,212,212)">()</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.old_provider </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> old_provider</span></div><br><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">createContextMenu</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.menu </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.old_provider.createContextMenu()</span></div><div><span style="color:rgb(212,212,212)">        <span style="color:rgb(102,102,102)"># make my modifications here</span><br></span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.a </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> QAction(</span><span style="color:rgb(206,145,120)">'a'</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(86,156,214)">None</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.menu.addAction(</span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.a)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(197,134,192)">return</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.menu</span></div><br><div><span style="color:rgb(86,156,214)">class</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">MyPlugin</span><span style="color:rgb(212,212,212)">:</span></div><br><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">__init__</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(156,220,254)">iface</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.iface </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> iface</span></div><br><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">initGui</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.old_provider </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.iface.layerTreeView().menuProvider()</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.my_provider </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> CustomMenuProvider(</span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.old_provider)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.iface.layerTreeView().setMenuProvider(</span><span style="color:rgb(86,156,214)">self</span><span style="color:rgb(212,212,212)">.my_provider)</span></div><div><span style="color:rgb(212,212,212)">        </span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">unload</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(197,134,192)">pass</span></div></div>

</div><div><br></div><br><div><br></div><div><br></div><br><div><br></div></div></div>