1

I have a plugin which I want to add options to either the context menu when right clicking a layer at the Layers panel or the properties menu for that layer.

How can I call them from PyQGIS?
Can I add the options every time the plugin loads?
It feels like the properties menu should be qgis.gui.QgsLayerPropertiesWidget but it seems to be mapped by symbol layers and not map layers. I can't find any class that seems to fit the right click context menu in the layer panel.

How can I do this?

EDIT 1

Luis Perez's answer was what i wanted.
I meanwhile found the way to add actions to the right click context menu, so posting this here for anyone else looking

def printSomething():
    print('Yay a new action!')
    
customAction = QAction('Print Something Nice')
customAction.triggered.connect(printSomething)
iface.addCustomActionForLayerType(customAction,'',qgis.core.QgsMapLayerType(0),True)

This will add the action Print Something Nice to all the Vector layers in your project.

enter image description here

This can be further customized by connecting the action to other signals instead of triggered, like hovered for instance.

3
  • You cannot add a new option to the context menu, because it is created simultaneously when right clicking any layer. – Kadir Şahbaz Oct 1 '20 at 21:22
  • @KadirŞahbaz Apparently you can, stumbled upon it not long ago. check out the edit just made. – Dror Bogin Oct 2 '20 at 5:40
  • I missed that. I couldn't add it before. Thanks – Kadir Şahbaz Oct 2 '20 at 6:39
3

This question was answered here Getting Layer Properties of QGIS on standalone application using PyQGIS API

It seems that the layer properties dialog is not accessible via PyQGIS but its individual parts are.

For example, try this on a vector layer, especially as you point out the symbol

layer = iface.activeLayer()
d = QgsLayerPropertiesWidget(QgsSimpleLineSymbolLayer(), QgsLineSymbol(), layer)
d.showPanel()
3
  • Thank you, this is what I was looking for. – Dror Bogin Oct 2 '20 at 5:30
  • You're welcome, at your service. By the way, brilliant code that you shared on the menu when you right clicked on the layer – Luis Perez Oct 2 '20 at 12:48
  • Came by it by accident while trying to solve this on my own. It would have been better to be able to add a QPushButton or just a widget but using the QAction is fine too. – Dror Bogin Oct 2 '20 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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