9

I want to remove an existing option ("Save As") from context menu of QGIS layer, what I have found so far is, to add new action in the LegendItems, or overwrite completely with new options. But could not find any option using that I would be able to remove an existing default Context menu option.

What I have also seen that there is menuProvider() method available, but could not get any details with this method.

How can I remove existing "Save As" option from the context menu of QGIS?

closed as off-topic by PolyGeo Jul 18 at 6:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

1

You need override the current context Menu, using your custom class

Minimal sample code for remove "Export" Option

from qgis.gui import QgsLayerTreeViewMenuProvider

ltv = iface.layerTreeView()
mp=ltv.menuProvider()
cm = mp.createContextMenu()

for action in cm.actions():
    # Need text value ,this object not have ObjectName
    if action.text() == "Exportar":
        cm.removeAction(action);
    #print (action.objectName())

class CustomMenuProvider(QgsLayerTreeViewMenuProvider):

  def __init__(self, view, actions):
    QgsLayerTreeViewMenuProvider.__init__(self)
    self.view = view
    self.defaultActions = actions

  def createContextMenu(self):
    if not self.view.currentLayer():
      return None
    m = QMenu()
    m.addActions(self.defaultActions)
    return m


provider = CustomMenuProvider(ltv, cm.actions())
ltv.setMenuProvider(provider)
0

Menu "Settings" => "Interface Customization" in the Menus/mProjectMenu disable mActionSaveProjectAs

  • 1
    That is option to disable QGIS's project, but not the layer, is that ? – Ahsan Mukhtar Jan 22 at 11:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.