Skip to main content
added 85 characters in body; edited tags
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

I want to set different actions(context menu) for vectors,rasters rasters,etc etc.

class MyMenuProvider(QgsLayerTreeViewMenuProvider):
def __init__(self, view):
    QgsLayerTreeViewMenuProvider.__init__(self)
    self.view = view

  def createContextMenu(self):
    if not self.view.currentLayer():
      return None
    m = QMenu()
    m.addAction("Show Attribute Table", self.openAttributeTable)
    return m

  def openAttributeTable(self):
    #iface1.openFeatureForm(iface1.activeLayer(), feature, False)
    iface1.showAttributeTable(iface1.activeLayer())
class MyMenuProvider(QgsLayerTreeViewMenuProvider):
    def __init__(self, view):
        QgsLayerTreeViewMenuProvider.__init__(self)
        self.view = view
    
      def createContextMenu(self):
        if not self.view.currentLayer():
          return None
        m = QMenu()
        m.addAction("Show Attribute Table", self.openAttributeTable)
        return m
    
      def openAttributeTable(self):
        #iface1.openFeatureForm(iface1.activeLayer(), feature, False)
        iface1.showAttributeTable(iface1.activeLayer())

then using it as

 view = iface1.layerTreeView()
 provider = MyMenuProvider(view)
 view.setMenuProvider(provider)

But this sets the menu provider for all layers  , I want to have different actions for different layers.

I want to set different actions(context menu) for vectors,rasters,etc .

class MyMenuProvider(QgsLayerTreeViewMenuProvider):
def __init__(self, view):
    QgsLayerTreeViewMenuProvider.__init__(self)
    self.view = view

  def createContextMenu(self):
    if not self.view.currentLayer():
      return None
    m = QMenu()
    m.addAction("Show Attribute Table", self.openAttributeTable)
    return m

  def openAttributeTable(self):
    #iface1.openFeatureForm(iface1.activeLayer(), feature, False)
    iface1.showAttributeTable(iface1.activeLayer())

then using it as

 view = iface1.layerTreeView()
 provider = MyMenuProvider(view)
 view.setMenuProvider(provider)

But this sets menu provider for all layers  , I want to have different actions for different layers

I want to set different actions(context menu) for vectors, rasters, etc.

class MyMenuProvider(QgsLayerTreeViewMenuProvider):
    def __init__(self, view):
        QgsLayerTreeViewMenuProvider.__init__(self)
        self.view = view
    
      def createContextMenu(self):
        if not self.view.currentLayer():
          return None
        m = QMenu()
        m.addAction("Show Attribute Table", self.openAttributeTable)
        return m
    
      def openAttributeTable(self):
        #iface1.openFeatureForm(iface1.activeLayer(), feature, False)
        iface1.showAttributeTable(iface1.activeLayer())

then using it as

view = iface1.layerTreeView()
provider = MyMenuProvider(view)
view.setMenuProvider(provider)

But this sets the menu provider for all layers, I want to have different actions for different layers.

The answer works also for QGIS v3.0, so I'm removing tags about QGIS v2.x
Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180
Tweeted twitter.com/StackGIS/status/894160640239075328
Post Reopened by Kersten, ahmadhanb, Germán Carrillo, Oto Kaláb, Devdatta Tengshe
edited tags; edited title
Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

setting Setting different context menu actions for different layers in Qgis using pythoneach layer type with PyQGIS

added 798 characters in body
Source Link
drama
  • 377
  • 1
  • 12
Loading
Post Closed as "Not suitable for this site" by ahmadhanb, PolyGeo
Source Link
drama
  • 377
  • 1
  • 12
Loading